关于scrapy
Updated:
scrapy是python写的爬虫框架,可以快速进行网站的爬取。
详细文档见http://scrapy-chs.readthedocs.io/zh_CN/latest/intro/tutorial.html
目录结构
1 | scrapy startproject <工程名> |
会创建如下结构1
2
3
4
5
6
7
8
9
10<工程名>/
scrapy.cfg
<工程名>/
__init__.py
items.py
pipelines.py
settings.py
spiders/
__init__.py
...
- scrapy.cfg: 项目的配置文件
- tutorial/: 该项目的python模块。之后您将在此加入代码。
- tutorial/items.py: 项目中的item文件.
- tutorial/pipelines.py: 项目中的pipelines文件.
- tutorial/settings.py: 项目的设置文件.
- tutorial/spiders/: 放置spider代码的目录.
调试-Scrapy shell
在目录下运行1
scrapy shell 'url'
可快速对一个网址进行调试,详见http://scrapy-chs.readthedocs.io/zh_CN/latest/topics/shell.html
常见的是copy一个xpath并进行调试
Middleware中间件
可以添加代码来处理发送给Spiders 的response及spider产生的item和request。
命令行
- 创建项目 scrapy startproject
- 进入项目目录 cd
- 创建一个spider scrapy genspider [-t template]
- 运行爬虫 scrapy crawl
- 启动shell scrapy shell [url]