美文网首页
Scrapy 连接提取器 Link Extractors

Scrapy 连接提取器 Link Extractors

作者: 会爬虫的小蟒蛇 | 来源:发表于2022-07-31 13:58 被阅读0次

Link Extractors需要在 CrawlSpider 类中使用

连接提取器可以简化对简单网页的列表URL提起

快速创建命令:

scrapy genspider -t crawl spiderName xxx.com

单独引入:

from scrapy.linkextractors import LinkExtractor

接口信息:

class scrapy.linkextractors.lxmlhtml.LxmlLinkExtractor(allow=(), deny=(), allow_domains=(), deny_domains=(), deny_extensions=None, restrict_xpaths=(), restrict_css=(), tags=('a', 'area'), attrs=('href', ), canonicalize=True, unique=True, process_value=None)
  • allow:通过正则表达式 提取URL

  • restrict_xpaths:通过Xpath 提取URL

  • restrict_css:通过Css选择器 提取URL

  • process_value: 对提取出的URL进行进一步处理

  • strip: 提取 的 地址 前后多余的空格删除

实例:

from scrapy.spiders import CrawlSpider, Rule

class TestSpider(CrawlSpider):
    name = 'test'
    # allowed_domains = ['ssr1.scrape.center']
    start_urls = ['http://ssr1.scrape.center/']
    rules = (
        Rule(LinkExtractor(attrs=('href',),  process_value="parse_url"), callback="parse_item", follow=False, ),
    )

    def parse_item(self, response):
        print(response.css(".m-b-sm::text").extract_first())
        # pass

    def parse_url(self, url):
        return "https://ssr1.scrape.center/" + url

相关文章

网友评论

      本文标题:Scrapy 连接提取器 Link Extractors

      本文链接:https://www.haomeiwen.com/subject/ckjkwrtx.html