Scrapy 2.6 Link Extractors 链接提取器使用指南

Python3Scrapy 爬虫框架 中数据爬取过程中链接提取器是从响应中提取链接的对象。从对象LxmlLinkExtractor.extract_links 返回匹配 Link对象的列表Response。链接提取器CrawlSpider通过一组Rule对象用于spider中。

Scrapy 版本:2.6+

# 实例化回调每一个链接信息def parse(self, response):    for link in self.link_extractor.extract_links(response):        yield Request(link.url, callback=self.parse)

链接提取器方法

LxmlLinkExtractor 函数方法

from scrapy.linkextractors import LinkExtractorclass scrapy.linkextractors.lxmlhtml.LxmlLinkExtractor(    allow=(),     deny=(),     allow_domains=(),     deny_domains=(),     deny_extensions=None,     restrict_xpaths=(),     restrict_css=(),     tags=('a', 'area'),     attrs=('href'),     canonicalize=False,     unique=True,     process_value=None,    strip=True)

应用举例

def parse(self, response):    link_extractor = LinkExtractor(allow=(r'xxxxx/\s+/#39;,), )    links = link_extractor.extract_links(response)    print(links)    for link in links:        print(link.url, link.text)



参数说明