要爬取每一页,可以使用正则表达式来匹配每一页的链接。

假设要爬取的网站的每一页的链接形式为 "https://www.kuaidaili.com/free/inha/{页码}/",可以使用以下正则表达式来匹配每一页的链接:

import re

allow_regex = r'https://www.kuaidaili.com/free/inha/\d+/'

然后在 Rule 的 allow 参数中使用这个正则表达式:

from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule

class MySpider(CrawlSpider):
    name = 'myspider'
    start_urls = ['https://www.kuaidaili.com/free/inha/1/']

    rules = (
        Rule(LinkExtractor(allow=allow_regex), callback='parse_item', follow=False),
    )

    def parse_item(self, response):
        # 解析每一页的内容
        pass

这样,Scrapy 将会根据这个规则爬取每一页的链接,并调用 parse_item 方法来解析每一页的内容

网站的每一页是httpswwwkuaidailicomfreeinha2httpswwwkuaidailicomfreeinha3等如何写 rules = RuleLinkExtractorallow= callback=parse_item follow=False 里面的allow参数爬取每一页

原文地址: http://www.cveoy.top/t/topic/hQWb 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录