基于twisted的异步回调

使得页面爬取有阶段性和连续性

from scrapy.http import Request
from urllib import parse

def parse(self, response):

    post_urls = response.css("a::attr(href)")

    for url in post_urls:

        # 有时候网站的href没有写全url,我们需要将域名添加到url前面,下面调用parse函数,拼接域名和资源路径

        yield Request(url=parse.urljoin(response.url, url), meta={}, callback=self.parse_detail)

    # 很多时候我们爬取的并不仅仅局限于一个网站上的内容,我们需要深度或广度遍历网站中的url,所以下边将继续对下一个url进行parse

    next_url = response.css(".next_page::attr(href)")

    if next_page:

        yield Request(url=parse.urljoin(response.url, url), meta={}, callback=self.parse)

def parse_detail(self, response):

    front_data = response.meta.get('xxx', '')
    pass

 

相关文章:

  • 2022-12-23
  • 2022-02-06
  • 2022-01-11
  • 2022-03-02
  • 2021-09-06
  • 2021-11-29
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-19
  • 2021-11-23
  • 2021-08-01
相关资源
相似解决方案