CrawlSpider模板
crawlSpider
创建
CrawlSpider模板scrapy genspider -t crawl <爬虫名字> <域名>模板代码示例:
# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
class XxxSpider(CrawlSpider):
name = 'xxx'
allowed_domains = ['www.baidu.com']
start_urls = ['http://www.baidu.com'] rules = (
Rule(LinkExtractor(allow=r'Items/'), callback='parse_item', follow=True),
)
def parse_item(self, response):
i = {}
#i['domain_id'] = response.xpath('//input[@id="sid"]/@value').extract()
#i['name'] = response.xpath('//div[@id="name"]').extract()
#i['description'] = response.xpath('//div[@id="description"]').extract()
return iCrawlSpider继承自Spider类,除了(name, allowed_domains, start_urls)之外,还定义了rules
rules
CrawlSpider使用rules来定义爬虫的爬取规则,并将匹配后的url自动拼接完整构造成请求提交给引擎。所以在正常情况下,CrawlSpider不需要单独手动返回请求了。
在rules中包含一个或多个Rule对象,每个Rule对爬取网站的动作定义了某种特定操作,比如提取当前相应内容里的特定链接,是否对提取的链接跟进爬取,对提交的请求设置回调函数等。
如果多个rule匹配了相同的链接,则根据规则在本集合中被定义的顺序,第一个会被使用。
Rule对象的参数LinkExtracto链接提取器,用于提取需要爬取的链接callback回调函数,提取的url请求对应的响应的处理函数,函数名是一个字符型注意:当编写爬虫规则时,避免使用parse作为回调函数。由于CrawlSpider使用parse方法来实现其逻辑,如果覆盖了 parse方法,crawl spider将会运行失败。
follow是否跟进链接,True表示跟进,就是在请求的url页面,有满足这个规则的url会被继续提取,然后组成Request发送跟调度器排队继续请求process_links:指定该spider中哪个的函数将会被调用,从link_extractor中获取到链接列表时将会调用该函数。该方法主要用来过滤。process_request:指定该spider中哪个的函数将会被调用, 该规则提取到每个request时都会调用该函数。 (用来过滤request)
LinkExtractor
allow:满足括号中正则表达式的URL会被提取,如果为空,则全部匹配。deny:满足括号中正则表达式的URL一定不提取(优先级高于allow)。allow_domains:会被提取的链接的domains。deny_domains:一定不会被提取链接的domains。restrict_xpaths:使用xpath表达式,和allow共同作用过滤链接。
案例
crawlSpider爬取腾讯招聘
# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from craw_spider.items import PositionItem, DetailItem
class HrSpider(CrawlSpider):
name = 'hr'
allowed_domains = ['hr.tencent.com']
start_urls = ['https://hr.tencent.com/position.php?start=0']
rules = (
# 提起职位基本信息规则
Rule(LinkExtractor(allow=r'position\.php\?&start=\d+#a'),
callback='parse_item',
follow=True),
# 提取职位详情页规则
Rule(LinkExtractor(allow=r'position_detail\.php\?id=\d+'),
callback='parse_detail',
follow=False),
)
def parse_item(self, response):
item = PositionItem()
trs = response.xpath(
'//table[@class="tablelist"]/tr[@class="even"] | //table[@class="tablelist"]/tr[@class="odd"]')
for tr in trs:
item['position_name'] = tr.xpath('./td/a/text()').extract_first()
item['position_type'] = tr.xpath('./td[2]/text()').extract_first()
item['position_num'] = tr.xpath('./td[3]/text()').extract_first()
item['position_addr'] = tr.xpath('./td[4]/text()').extract_first()
item['publish_data'] = tr.xpath('./td[5]/text()').extract_first()
yield item
def parse_detail(self, response):
item = DetailItem()
item['position_require'] = response.xpath('//table[@class="tablelist textl"]/tr[3]/td/ul/li//text()').extract()
item['position_duty'] = response.xpath('//table[@class="tablelist textl"]/tr[4]/td/ul/li//text()').extract()
yield item
其他组件的使用和
Spider是一样的
CrawlSpider模板的更多相关文章
- python爬虫入门(八)Scrapy框架之CrawlSpider类
CrawlSpider类 通过下面的命令可以快速创建 CrawlSpider模板 的代码: scrapy genspider -t crawl tencent tencent.com CrawSpid ...
- Scrapy框架-CrawlSpider
目录 1.CrawlSpider介绍 2.CrawlSpider源代码 3. LinkExtractors:提取Response中的链接 4. Rules 5.重写Tencent爬虫 6. Spide ...
- Scrapy 使用CrawlSpider整站抓取文章内容实现
刚接触Scrapy框架,不是很熟悉,之前用webdriver+selenium实现过头条的抓取,但是感觉对于整站抓取,之前的这种用无GUI的浏览器方式,效率不够高,所以尝试用CrawlSpider来实 ...
- Scrapy框架——使用CrawlSpider爬取数据
引言 本篇介绍Crawlspider,相比于Spider,Crawlspider更适用于批量爬取网页 Crawlspider Crawlspider适用于对网站爬取批量网页,相对比Spider类,Cr ...
- scrapy爬取微信小程序社区教程(crawlspider)
爬取的目标网站是: http://www.wxapp-union.com/portal.php?mod=list&catid=2&page=1 目的是爬取每一个教程的标题,作者,时间和 ...
- CrawlSpiders
1.用 scrapy 新建一个 tencent 项目 2.在 items.py 中确定要爬去的内容 # -*- coding: utf-8 -*- # Define here the models f ...
- 三、scrapy后续
CrawlSpiders 通过下面的命令可以快速创建 CrawlSpider模板 的代码: scrapy genspider -t crawl tencent tencent.com 我们通过正则表达 ...
- scrapy入门与进阶
Scrapy是用纯Python实现一个为了爬取网站数据.提取结构性数据而编写的应用框架,用途非常广泛. 框架的力量,用户只需要定制开发几个模块就可以轻松的实现一个爬虫,用来抓取网页内容以及各种图片,非 ...
- scrapy框架整理
0.安装scrapy框架 pip install scrapy 注:找不到的库,或者安装部分库报错,去python第三方库中找,很详细 https://www.lfd.uci.edu/~gohlke/ ...
随机推荐
- VS2017提醒找不到MSVCR110D.dll
我的电脑时win10我已解决,不能传文件,需要联系我
- 逐帧动画 两种实现方式 css和js
第一种: css部分: <style> #foxtail{ background: url(../img/foxtail.png) 0 0 no-repeat; width: 156px; ...
- idea 工具中项目文件上有灰色的小X号去除方法
初使用idea,在项目中发现类上有这样的灰色X号,启动项目后idea会报找不到这个类的错误,原因是它没有被编译, 解决方法 setting->Build->Compiler->Exc ...
- python笔记26-编码规范层级目录
bin-放的可执行文件 conf-放的配置文件 lib-放的一些lib库 temp-放的零时文件 logs-日志 core-核心逻辑 data-存放数据 README-帮助文档 start_shop. ...
- java 秒时间格式化
public static String durationFormat(Integer totalSeconds) { if (totalSeconds == null || totalSeconds ...
- vs问题--------------标记为系统必备组建...
问题:标记为系统必备组建 要将程序集“D:\project\DMS\DMSGaeaService\TmsApplication\bin\Debug\Jns.Gaea.dll”标记为系统必备组件,必须对 ...
- AutoCAD 2019.0.1 Update 官方简体中文版
欧特克三维机械设计软件AutoCAD 2019版本于2018年3月23号全球正式发布,新版本图标全新设计,视觉效果更清晰:在功能方面,全新的共享视图功能.DWG文件比较功能:现在打开及保存图形文件已经 ...
- linux中sed命令批量修改
sed命令下批量替换文件内容 格式: sed -i "s/查找字段/替换字段/g" `grep 查找字段 -rl 路径` 文件名 -i 表示inplace edit,就地修改文件 ...
- JS中的一元操作符
表达式 一元操作符 优先级 结合性 运算顺序 表达式是什么? 就是JS 中的一个短语,解释器遇到这个短语以后会把对它进行计算,得到一个结果参与运算,我们把这种要参与到运算中的各种各样的短语称为表达式. ...
- 泊爷带你学go -- redis连接池的操作
package main import ( "common" "fmt" "proto" "strconv" " ...