3

一般来说,我了解如何使用 Scrapy 和 x-path 来解析 html。但是,我不知道如何获取 HAR 数据。

mport scrapy
from scrapy_splash import SplashRequest

class QuotesSpider(scrapy.Spider):
    name = 'quotes'
    allowed_domains = ['quotes.toscrape.com']
    start_urls = ['http://quotes.toscrape.com/js']

    def start_requests(self):
        for url in self.start_urls:
            yield SplashRequest(url=url,
                                callback=self.parse,
                                endpoint='render.html')


    def parse(self, response):
        quotes = response.xpath('//*[@class="quote"]')
        for quote in quotes:
            yield { 'author': quote.xpath('.//*[@class="author"]/text()').extract_first(),
                    'quote': quote.xpath('.//*[@class="text"]/text()').extract_first()
                    }

        script = """function main(splash)
                assert(splash:go(splash.args.url))
                splash:wait(0.3)
                button = splash:select("li[class=next] a")
                splash:set_viewport_full()
                splash:wait(0.1)
                button:mouse_click()
                splash:wait(1)
                return {url = splash:url(),
                        html = splash:html(),
                        har = splash:har()}
            end """
        yield SplashRequest(url=response.url,
                                callback=self.parse,
                                endpoint='execute',
                                args={'lua_source':script})

脚本中将 HAR 数据导出到文件的下一条语句是什么?如何将所有网络数据生成到文件中?任何见解将不胜感激。

4

1 回答 1

0
response.data['har']

在你解析方法中。

于 2021-09-04T09:58:30.257 回答