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/ ...
随机推荐
- MATLAB中a(:)和." ' "," ' "
今天继续学习matlab看书时发现了一行代码 a=a(:).'; 起初可能不懂就百度一下 a(:)可以将向量转换为列向量,行向量相当于直接转置,列向量不变 而如果是n*m维向量则仍然是以列为主,即按照 ...
- Linux快捷键 Linux权限
第1章 回顾昨天内容 1.1 取出网卡ip地址 取出文件权限 1.2 awk '找谁{干啥}' awk 'NR==2{print $4}' 1.3 系统时间 [root@oldboyedu-40-n ...
- 不同版本的IDE ,对应的选项 有变化
xx.dproj对应的项目级选项 也不同,所以,要分别保存为不同的文件. 如果不同的IDE,打开同一个 .dproj文件,会因为 选项界面的选项不同,提示一些错误.
- Ubuntu下安装chrome浏览器
1.在终端中,输入以下命令: sudo wget http://www.linuxidc.com/files/repo/google-chrome.list -P /etc/apt/sources.l ...
- 基于mykernel完成多进程的简单内核
学号351 原创作品转载请注明出处 + https://github.com/mengning/linuxkernel/ mykernel简介 mykernel是由孟宁老师建立的一个用于开发您自己的操 ...
- saltstack高效运维
saltstack高效运维 salt介绍 saltstack是由thomas Hatch于2011年创建的一个开源项目,设计初衷是为了实现一个快速的远程执行系统. salt强大吗 系统管理员日常会 ...
- json、demjson
一.json 概述: json.dumps():将 Python 对象编码成 JSON 字符串, dic -> json str json.dump() :将 Python 对象保存成 JSO ...
- lsof 命令用法详解
lsof 命令用法详解 作用 用于查看你进程开打的文件,打开文件的进程,进程打开的端口(TCP.UDP).找回/恢复删除的文件.是十分方便的系统监视工具,因为lsof命令需要访问核心内存和各种文件,所 ...
- xcode的打包上线出问题:导入此构建版本时出错
原因:升级mac系统到了High sierra(10.13,目前还是测试版,并没有正式版,全新的文件系统 APFS (Apple File System))发现没有这个问题,于是乎,所以给出这种解决方 ...
- sqoop碰到的问题
sqoop碰到的问题 背景:从Oracle接入数据,一张表一千多万,数据量13G左右. 报错,表名找不到,将表名改成大写的 Exception in thread "main" j ...