scrapy 牛
scrapy是一个非常强大的框架,我们可以用它在网络上自由自在的爬取各种数据,当然,要合法啦,爬到的数据可以持久化在本地文件/CSV文件/json文件/mysql数据库/mongodb数据库.....等等
存入CSV文件/json文件中操作比较简单,只需在命令行中启动项目的时候在启动时输入即可,或者有其他方法,可以自行百度,当然,还是建议查看官方文档,毕竟官方出品,必属精品
···
scrapy runspider quotes_spider.py -o quotes.json
···
此次我就存入mongodb数据库详细写一下,后期还会增加存入MySQL数据库的操作
由于这个demo比较简单,所以很容易理解
我这个是爬取的雪球网,数据库的名称为:spider_1129, 表的名称为:xueqiu
items.py 中的操作

xueqiu.py 中操作

settings.py 中操作
请求头需要加User-agent,Referer方可访问,还要在middlewares.py中自己写cookies中间件,不然通不过服务器的验证,无法获取数据



pipelines.py 中操作

middlewares.py 中的操作
需重写中间件,在向服务器发送请求的时候把需要的请求头数据发送过去,尤其是cookie

额外增加 可优化启动项目,无需在命令行执行scrapy crawl xxxx 只需在自定义的main.py中右键启动即可

网友评论