Scrapy爬虫中的链接提取器LinkExtractor

今天在编写Scrapy爬虫的时候接触到了LinkExtractor,遂学习了一下这个链接提取器。

Link Extractors 是那些目的仅仅是从网页(scrapy.http.Response 对象)中抽取最终将会被follow链接的对象。使用场景就是在一个网站中通过自定义规则提取到自己想要的那些网址。

Scrapy默认提供2种可用的 Link Extractor, 但你通过实现一个简单的接口创建自己定制的Link Extractor来满足需求。默认的LinkExtractor(也就是LxmlLinkExtractor)拥有比较方便的过滤选项,使用LXML的强大的HTMLParser实现。

使用的时候先从scrapy.linkextractors模块中引入:

from scrapy.linkextractors import LinkExtractor

LxmlLinkExtractor的使用:

class scrapy.contrib.linkextractors.lxmlhtml.LxmlLinkExtractor(allow=(), deny=(), allow_domains=(), deny_domains=(), deny_extensions=None, restrict_xpaths=(), tags=('a', 'area'), attrs=('href', ), canonicalize=True, unique=True, process_value=None)

相关参数的含义请参考文档:http://scrapy-chs.readthedocs.io/zh_CN/0.24/topics/link-extractors.html

主要参数:

allow: #满足括号中正则表达式的值会被提取,如果为空则全部匹配
allow_domains: #会被提取的链接的域名

与之相关的CrawlSpider以及Rule之后应该会一起整合写一个博客。

猜你喜欢

转载自www.cnblogs.com/EdenChanIy/p/9398758.html