- 安装wheel
- 安装lxml
- 安装PyOpenssl
- 安装Twisted :异步框架
- 安装Pywin32
- 安装Scrapy
通过 scrapy startproject projectname 创建一个scrapy工程
进入该文件夹:
scrapy genspider baidu www.baidu.com 生成爬虫
scrapy crawl baidu 运行
scrapy shell quotes.toscrape.com 进入命令行交互模式
QuoteItem 处理返回值的数据
scrapy crawl quotes -o quotes.json 将数据输出到quotes.json文件中
scrapy crawl quotes -o quotes.jl 将数据输出到quotes.jl,每一段数据存成一行
-o 输出文件.格式
csv xls Marshall ftp://user:password@ftp.example.com/path/quotes.csv
通过pipeline.py里的配置,可将数据输入到数据库中
Scrapy 命令行详解
Global commands:
● startproject
● genspider
● settings
● runspider
● shell
● fetch
● view
● version
Project-only commands:
● crawl
● check
● list
● edit
● parse
● bench
scrapy选择器用法:
https://docs.scrapy.org/en/latest/topics/selectors.html
scrapy spiders用法:
Spider类定义了如何爬取某个(或某些)网站。包括了爬取的动作(例如:是否跟进链接)以及如何从网页的内容中提取结构化数据(爬取item)。 换句话说,Spider就是您定义爬取的动作及分析某个网页(或者是有些网页)的地方。
https://docs.scrapy.org/en/latest/topics/spiders.html
https://scrapy-chs.readthedocs.io/zh_CN/latest/topics/spiders.html
scrapy itemPipeline用法:
清洗数据
处理数据:
存入文件/数据库等
https://docs.scrapy.org/en/latest/topics/item-pipeline.html
https://scrapy-chs.readthedocs.io/zh_CN/latest/topics/item-pipeline.html
scrapy downloadMiddleware
下载器中间件是介于Scrapy的request/response处理的钩子框架。 是用于全局修改Scrapy request和response的一个轻量、底层的系统。
https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
https://scrapy-chs.readthedocs.io/zh_CN/latest/topics/spider-middleware.html