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/ ...
随机推荐
- Spring MVC扩展
使用@ResonseBody实现异步请求时返回的数据对象的输出. 通过配置StringHttpMessageConverter消息转换器来解决JSON数据传递中出现的中文乱码问题. 在实际项目开发中, ...
- Python中对字符串的操作
Python字符串的相关操作 1.字符串格式判断 s.isalnum() #所有字符都是数字或者字母 s.isalpha() #所有字符都是字母 s.isdigit() #所有字符都是数字 s.isl ...
- windows+nginx 查看并发链接数
1.windows下nginx查看并发链接数要使用stable版本 2.配置代码: location /status { stub_status on; } 3.访问地址:http://localho ...
- linux下C获取文件的大小
获取文件大小这里有两种方法: 方法一. 范例: unsigned long get_file_size(const char *path) { unsigned long filesize = -1; ...
- linux(ubuntu)GCC编译包含库函数的问题
GCC 编译命令通常为:gcc hello.c -o hello.out 注意:若hello.c中引用有库函数(比如math.h),直接编译会出错 "/tmp/ccalvMPY.o: In ...
- 凯撒密码移位python
#!/usr/bin/python'''凯撒密码'''a="gmbhqwertghjkcvbzn"s=[""]*len(a)for j in range(26) ...
- java——类、对象、方法
一.类 1.Java语言把一组对象中相同属性和方法抽象到一个Java源文件就形成了类. 一个java文件可以有多个类,但是每一个类都会生成一个class字节码文件. 如果class 前加public ...
- 关于js数组的简单复制
var a=[]; a.push(1); a.push(2); a.push(3); var b=a; b[0]=4; alert(a);//4,2,3 alert(b);//4,2,3 这种写法由于 ...
- shiro三连斩之第一斩
通过JavaSE,创建不同的 realm ,由简单到复杂一步步的深入的理解shiro完成认证与授权内在联系 推荐从下向上一步步的测试,每一个方法都有详细的注释,说明 从哪里来-->到哪里去,理 ...
- Python 习题一
1.使用while循环输入 1 2 3 4 5 6 8 9 10 # Author:Tony.lou i = 1 while i < 11: if i == 7: pass else: prin ...