scrapy--Rule()与LinkExtractor()函数理解
这两个函数用于CrawlSpider内的rules属性中,具体的参数用法网上有很多,这里不再赘述。我想说的是差点搞死我的几个注意点。
1.来源:
from scrapy.contrib.spiders import Rule
from scrapy.linkextractors import LinkExtractor
2.注意点:
1.rules内规定了对响应中url的爬取规则,爬取得到的url会被再次进行请求,并根据callback函数和follow属性的设置进行解析或跟进。
这里强调两点:一是会对所有返回的response进行url提取,包括首次url请求得来的response;二是rules列表中规定的所有Rule都会被执行。
2.allow参数没有必要写出要提取的url完整的正则表达式,部分即可,只要能够区别开来。且最重要的是,即使原网页中写的是相对url,通过LinkExtractor这个类也可以提取中绝对的url,这个类太厉害了。
3.LinkExtractor单独使用
start_urls = ['https://www.kanunu8.com/book2/10935/index.html']
def parse(self, response):link = LinkExtractor(allow='\d{6}\.html',restrict_xpaths='//div//table//a')links = link.extract_links(response)print(links)
[Link(url=‘https://www.kanunu8.com/book2/10935/194600.html’, text=‘楔子’, fragment=’’, nofollow=False),
Link(url=‘https://www.kanunu8.com/book2/10935/194601.html’, text=‘第一章’, fragment=’’, nofollow=False),
Link(url=‘https://www.kanunu8.com/book2/10935/194602.html’, text=‘第二章’, fragment=’’, nofollow=False),
Link(url=‘https://www.kanunu8.com/book2/10935/194603.html’, text=‘第三章’, fragment=’’, nofollow=False),
Link(url=‘https://www.kanunu8.com/book2/10935/194604.html’, text=‘第四章’, fragment=’’, nofollow=False),
Link(url=‘https://www.kanunu8.com/book2/10935/194605.html’, text=‘第五章’, fragment=’’, nofollow=False),
Link(url=‘https://www.kanunu8.com/book2/10935/194606.html’, text=‘第六章’, fragment=’’, nofollow=False),
Link(url=‘https://www.kanunu8.com/book2/10935/194607.html’, text=‘第七章’, fragment=’’, nofollow=False),
Link(url=‘https://www.kanunu8.com/book2/10935/194608.html’, text=‘第八章’, fragment=’’, nofollow=False),
Link(url=‘https://www.kanunu8.com/book2/10935/194609.html’, text=‘第九章’, fragment=’’, nofollow=False),
Link(url=‘https://www.kanunu8.com/book2/10935/194610.html’, text=‘第十章’, fragment=’’, nofollow=False),
Link(url=‘https://www.kanunu8.com/book2/10935/194611.html’, text=‘第十一章’, fragment=’’, nofollow=False),
Link(url=‘https://www.kanunu8.com/book2/10935/194612.html’, text=‘第十二章’, fragment=’’, nofollow=False),
Link(url=‘https://www.kanunu8.com/book2/10935/194613.html’, text=‘第十三章’, fragment=’’, nofollow=False),
Link(url=‘https://www.kanunu8.com/book2/10935/194614.html’, text=‘后记’, fragment=’’, nofollow=False)]
看到没,原网页给的是相对地址,它竟然能够通过计算返回出绝对地址,真是很厉害。而且links是一个Link对象的列表。这里通过:
for link in links:
print(link.url)
即可提取绝对url地址,这个作用很方便,就不用再用response.urljoin()函数了。
scrapy--Rule()与LinkExtractor()函数理解相关推荐
- scrapy rule follow的理解和应用
follow 是一个布尔(boolean)值,指定了根据该规则从response提取的链接是否需要跟进. 如果callback 为None,follow 默认设置为 True ,添加回调函数callb ...
- nodejs回调函数理解
回调实例 问题:想要得到一秒后 计算出的结果 //错误写法function add(x,y) {console.log(1);setTimeout(function () {console.log(2 ...
- ML之MIC:利用有无噪音的正余弦函数理解相关性指标的不同(多图绘制Pearson系数、最大信息系数MIC)
ML之MIC:利用有无噪音的正余弦函数理解相关性指标的不同(多图绘制Pearson系数.最大信息系数MIC) 目录 利用有无噪音的正余弦函数理解相关性指标的不同(多图绘制Pearson系数.最大信息系 ...
- 高频交易配对交易学习——Copulas函数理解
Copulas函数理解 https://github.com/MalteKurz/VineCopulaCPP
- Pytorch中tensor.view().permute().contiguous()函数理解
Pytorch中tensor.view().permute().contiguous()函数理解 yolov3中有一行这样的代码,在此记录一下三个函数的含义 # 例子中batch_size为整型,le ...
- pytorch中repeat()函数理解
pytorch中repeat()函数理解 最近在学习过程中遇到了repeat()函数的使用,这里记录一下自己对这个函数的理解. 情况1:repeat参数个数与tensor维数一致时 a = torch ...
- SQLServer STUFF 函数理解
SQLServer CAST -- 转换数据类型 逗号表示分割 . STUFF 函数理解 -- 第一个就是字符串 FOR XML PATH('') 必须用 , 第二个参数 负数或0空字符串, ...
- Java回调函数理解和应用
#Java回调函数理解和应用 所谓回调:就是A类中调用B类中的某个方法C,然后B类中反过来调用A类中的方法D,D这个方法就叫回调方法,这样子说你是不是有点晕晕的. 在未理解之前,我也是一脸懵逼,等我理 ...
- pytorch 中 contiguous() 函数理解
pytorch 中 contiguous() 函数理解 文章目录 pytorch 中 contiguous() 函数理解 引言 使用 contiguous() 后记 文章抄自 Pytorch中cont ...
- Android回调函数理解
Android回调函数理解,比如我用一个activity去做显示下载进度的一个进度条,但是下载是另外一个B类来做的,这个时候我Activity获取下载的进度就可以提供一个回调接口,然后让下载类来回调就 ...
最新文章
- PostgreSql 功能和操作
- XamarinEssentials教程首选项Preferences判断项目是否存在
- pythonvim编辑教程_使用vim编辑python
- Visual Studio原生开发的20条调试技巧
- XP 安装Oralce 10g 数据库
- 防止过拟合以及解决过拟合
- 出现画面抖动_连续抖动20小时!虎门大桥桥面如波浪翻滚,专家:个人感觉没问题...
- 作者:赵雷,山东农业大学硕士生。
- 怎么拆除境地柜_内衣不合身拒绝接亲,精装房装修不满意怎么办?
- RFC 5961翻译
- 关于键盘(总论8042)
- python下载网页歌曲
- server 2016备份还原
- 《因果学习周刊》第13期:ICLR 23因果推断高分论文
- 查看oracle配置信息,查看 Oracle Solaris 系统配置信息
- 对Socket CAN的理解(1)——【CAN总线原理】
- 嵌入式分享合集144
- 树莓派python学习篇 (二)红外避障传感器
- java jodatime明天_使用Joda-Time优雅的处理日期时间
- python教程 w3c_W3C 教程
热门文章
- 案例详解:理解Python中的“解析式”
- 飓风桑迪:曼哈顿数据中心的灾难应急方案
- 区块链加持的家用摄像头能拯救你的隐私吗?
- 他们都说springboot是懒人神器,你觉得呢?
- SysWow64没有权限解决办法
- cf/codeforces #365 E - Mishka and Divisors 数学+背包dp+gcd
- miui修改Android,修改 MIUI「快捷开关」布局
- 肿瘤NGS的常规检测流程
- Android verified boot 2.0 vbmeta 数据结构解析
- 圈内著名ts_TS欠薪几百万遭实锤,阿泰被玩弄于股掌,权谋剧情比宫斗还精彩...