1、初识scrapy
2017-04-24 14:32
190 查看
Scrapy是一个从网上爬取数据的开源的、友好的框架。
An open source and collaborative framework for extracting the data from website.
scrapy具有以下优点:
1. 快速强大
2. 容易扩展
3. 适应于多平台
stackoverflow_spider.py
运行命令
该爬虫的运行过程:
1. 使用start_urls作为初始url生成Request,并默认把parse作为它的回调函数。
2. 在parse中采用css选择器获得目标URL,并注册parse_question作为目标URL的回调函数。
交互式控制台用于调试数据抽取方法(scrapy shell)
内置对爬取结果的输出支持,可以将结果保存为json/csv/xml等
自动处理编码
支持自定义扩展
丰富的内置扩展,可用于处理:
cookies and session
http features like compression, authentication,caching
user-agent spoofing
robots.txt
crawl depth restriction
An open source and collaborative framework for extracting the data from website.
scrapy具有以下优点:
1. 快速强大
2. 容易扩展
3. 适应于多平台
一个简单的爬虫
下面这个爬虫爬取了stackoverflow问题页中的内容。stackoverflow_spider.py
import scrapy class StackoverflowSpider(scrapy.Spider): name = 'stackoverflow' start_urls=['http://stackoverflow.com/questions?sort=votes'] def parse(self, response): for href in response.css('.question-summary h3 a::attr(href)'): full_url = response.urljoin(href.extract()) yield scrapy.Request(full_url, callback=self.parse_question) def parse_question(self, response): yield { 'title': response.css('h1 a::text').extract()[0], 'votes': response.css('.question .vote-count-post::text').extract()[0], 'body': response.css('.question .post-text').extract()[0], 'tags': response.css('.question .post-tag::text').extract(), 'link': response.url }
运行命令
scrapy runspider stackoverflow_spider.py -o result.csv,将爬虫爬取结果保存到result.csv中。
该爬虫的运行过程:
1. 使用start_urls作为初始url生成Request,并默认把parse作为它的回调函数。
2. 在parse中采用css选择器获得目标URL,并注册parse_question作为目标URL的回调函数。
scrapy的高级特性
内置css/xpath/re数据抽取器交互式控制台用于调试数据抽取方法(scrapy shell)
内置对爬取结果的输出支持,可以将结果保存为json/csv/xml等
自动处理编码
支持自定义扩展
丰富的内置扩展,可用于处理:
cookies and session
http features like compression, authentication,caching
user-agent spoofing
robots.txt
crawl depth restriction
相关文章推荐
- [置顶] Python3网络爬虫(十二):初识Scrapy之再续火影情缘
- Scrapy: 初识Scrapy
- Python3网络爬虫:初识Scrapy爬虫框架
- 初识scrapy
- 爬虫学习笔记-Scrapy初识
- 初识Scrapy-实战(一)
- 初识 scrapy 框架 - 安装
- 初识Scrapy框架——在Pycharm上安装Scrapy
- scrapy框架系列 (1) 初识scrapy
- 初识Scrapy框架——在Pycharm上安装Scrapy
- 初识scrapy及scrapy 小爬虫程序实练
- Python---初识Scrapy框架
- capwap学习笔记——初识capwap(二)
- Linux搭建Scrapy爬虫集成开发环境
- Scrapy爬虫入门系列2 示例教程
- Scrapy研究探索(七)——如何防止被ban之策略大集合
- 初识数据库之红皮书——问题集锦
- Dagger2 注解 初识
- scrapy递归抓取网页数据
- 初识PHP设计模式--职责链模式