制作Scrapy爬虫
1、新建项目(命令行中输入:scrapy startproject xxx):新建一个爬虫项目
2、使用pycharm打开项目,查看项目目录

3、明确目标(编写item.py:放数据模型代码):明确你要抓取的目标

4、制作爬虫(spider/xxspider.py):制作爬虫开始爬取网页
(1)创建爬虫文件,spider目录下就会多出这个文件xxspider.py
scrapy genspider xxx xxx.com
(2)编写爬虫文件,处理请求和响应,以及提取数据(yield item)

爬取内容:
①name = 'tencent' #爬虫名,启动爬虫需要的参数,必须
②allowed_domains = ['tencent.com']#爬取域范围,允许爬虫在这个域名下进行爬取(可选)
③start_urls = []#起始URL列表,爬虫执行后的第一批请求,将从这个列表里获取
5、存储内容(编写管道文件pipelines.py):设计管道存储爬取内容,处理spider返回item数据,比如本地持久化存储
6、编写setting.py设置文件,启动管道组件,以及相关设置

7、执行爬虫
scrapy crawl xxx
8、爬虫拿到的数据保存信息的四种方法,-o输出指定格式的文件
(1)json格式:默认unicode编码--scrapy crawl xxx -o xxx.json
(2)json lines格式:默认unicode编码--scrapy crawl xxx -o xxx.jsonl
(3)csv逗号表达式,可用Excel打开--scrapy crawl xxx -o xxx.csv
(4)xml格式--scrapy crawl xxx -o xxx.xml
网友评论