网站线框,毕业设计报告网站开发,旅游网站制作建设,网站备案信息是什么意思#xff08;3#xff09;在 pipeline.py 文件中对数据进行存储#xff0c;此程序先写 入 txt 文件中#xff0c;是为了判断该程序是否能正确爬取出数据。 此处使用了 json 库#xff0c;使用 ensure_ascii False#xff0c;能够确 保非 ASCII 字符#xff08;如中文3在 pipeline.py 文件中对数据进行存储此程序先写 入 txt 文件中是为了判断该程序是否能正确爬取出数据。 此处使用了 json 库使用 ensure_ascii False能够确 保非 ASCII 字符如中文的数据写入 txt 文件中。
import json
class DoubanPipeline:def open_spider(self,spider):self.f open(maoer1.json,w,encodingutf-8)def process_item(self, item, spider):json_str json.dumps(dict(item),ensure_asciiFalse) \nself.f.write(json_str)return itemdef close_spider(self,spider):self.f.close()
4在 setting.py 文件中设置优先级。 此外在我调试的过程中我发现得做反爬措施。 5在此项目下创建一个 main.py 文件用于调试。
import os.path
import sys
from scrapy.cmdline import execute
currentFile os.path.abspath(__file__)
currentPath os.path.dirname(currentFile)
# print(currentPath)
sys.path.append(currentPath)
execute([scrapy,crawl,db])
6最终得到的数据如下json 文件中
7将数据转存至 mysql 中使用 pymysql 成功连接数据 库后通过 sql 语句 insert into 表名 values值将数 据进行保存。
import mysql.connector
import jsonconn mysql.connector.connect(host127.0.0.1,userroot,password010208,databasespider,port 3306,charset utf8
)cursor conn.cursor()with open(maoer1.json, r) as file:data json.load(file)for entry in data:description entry.get(description, ) # 确保title字段存在movie_name entry.get(movie_name, )director entry.get(director, )score entry.get(score, )sql INSERT INTO spider10 (description,movie_name,director,score) VALUES (%s,%s,%s,%s)cursor.execute(sql, (description,movie_name,director,score))
conn.commit()cursor.close()
conn.close() 8结果展示 三.数据可视化
本题根据现有数据做了柱状图和词云图。好像不是很好看 四.应用场景
通过对豆瓣网站进行数据爬取并进行可视化分析我们可以看到当代社会中人们喜欢的影视作品种类多样评分较高质量较高。希望该网站进行推出优秀作品丰富人们的闲暇生活。 ok这就是完整的程序说明重点我自己写的