如何抓取这种动态生成的网站数据?

时间:2019-07-07 08:46:49

标签: python scrapy scrapy-splash

我正在尝试抓取电子商务网站, 范例连结:https://www.lazada.sg/products/esogoal-2-in-1-selfie-stick-tripod-bluetooth-selfie-stand-with-remote-shutter-foldable-tripod-monopod-i279432816-s436738661.html?mp=1

数据是通过React呈现的,当我在几个链接上执行抓取时,大多数数据都以null的形式返回,并且当我查看页面源代码时,我实际上找不到通过inspect元素可用的HTML,只是Javascript标签内的json。我测试了几次在相同的链接和未曾发现的数据上运行scrapy scrapper,实际上返回了内容,因此以某种方式是随机的。我不知道该如何抓取这种网站。 同样,我正在使用用户代理池并在请求之间中断。

 script = '''
        function main(splash, args)
            assert(splash:go(args.url))
            assert(splash:wait(1.5))
            return splash:html()
        end
    '''

    def start_requests(self):
        url= [
            'https://www.lazada.sg/products/esogoal-tactical-sling-bag-outdoor-chest-pack-shoulder-backpack-military-sport-bag-for-trekking-camping-hiking-rover-sling-daypack-for-men-women-i204814494-s353896924.html?mp=1',
            'https://www.lazada.sg/products/esogoal-2-in-1-selfie-stick-tripod-bluetooth-selfie-stand-with-remote-shutter-foldable-tripod-monopod-i279432816-s436738661.html?mp=1',
            'https://www.lazada.sg/products/esogoal-selfie-stick-tripod-extendable-selfie-stick-monopod-with-integrated-tripod-and-bluetooth-remote-shutter-wireless-selfie-stick-tripod-for-cellphonecameras-i205279097-s309050125.html?mp=1',
            'https://www.lazada.sg/products/esogoal-mini-umbrella-travel-umbrella-sun-rain-umbrella8-ribs-98cm-big-surface-lightweight-compact-parasol-uv-protection-for-men-women-i204815487-s308312226.html?mp=1',
            'https://www.lazada.sg/products/esogoal-2-in-1-selfie-stick-tripod-bluetooth-selfie-stand-with-remote-shutter-foldable-tripod-monopod-i279432816-s436738661.html?mp=1'
        ]

        for link in url:
            yield SplashRequest(url=link, callback=self.parse, endpoint='render.html', args={'wait' : 0.5, 'lua_source' : self.script}, dont_filter=True)

 def parse(self, response):
        yield {
            'title' : response.xpath("//span[@class='pdp-mod-product-badge-title']/text()").extract_first(),
            'price' : response.xpath("//span[contains(@class, 'pdp-price')]/text()").extract_first(),
            'description' : response.xpath("//div[@id='module_product_detail']").extract_first()
        }

1 个答案:

答案 0 :(得分:1)

我尝试:

  • 将“ execute”作为初始方法的参数传递,而不是“ render html”

    from scrapy_splash import SplashRequest
    
    class DynamicSpider(scrapy.Spider):
    name = 'products'
    url = [
        'https://www.lazada.sg/products/esogoal-tactical-sling-bag-outdoor-chest-pack-shoulder-backpack-military-sport-bag-for-trekking-camping-hiking-rover-sling-daypack-for-men-women-i204814494-s353896924.html?mp=1',
        'https://www.lazada.sg/products/esogoal-2-in-1-selfie-stick-tripod-bluetooth-selfie-stand-with-remote-shutter-foldable-tripod-monopod-i279432816-s436738661.html?mp=1',
        'https://www.lazada.sg/products/esogoal-selfie-stick-tripod-extendable-selfie-stick-monopod-with-integrated-tripod-and-bluetooth-remote-shutter-wireless-selfie-stick-tripod-for-cellphonecameras-i205279097-s309050125.html?mp=1',
        'https://www.lazada.sg/products/esogoal-mini-umbrella-travel-umbrella-sun-rain-umbrella8-ribs-98cm-big-surface-lightweight-compact-parasol-uv-protection-for-men-women-i204815487-s308312226.html?mp=1',
        'https://www.lazada.sg/products/esogoal-2-in-1-selfie-stick-tripod-bluetooth-selfie-stand-with-remote-shutter-foldable-tripod-monopod-i279432816-s436738661.html?mp=1',
    ]
    
    script = """
        function main(splash, args)
          assert(splash:go(args.url))
          assert(splash:wait(1.5))
          return {
            html = splash:html()
          }
        end
    """
    
    def start_requests(self):
        for link in self.url:
            yield SplashRequest(
                url=link,
                callback=self.parse,
                endpoint='execute',
                args={'wait': 0.5, 'lua_source': self.script},
                dont_filter=True,
            )
    
    def parse(self, response):
        yield {
            'title': response.xpath("//span[@class='pdp-mod-product-badge-title']/text()").extract_first(),
            'price': response.xpath("//span[contains(@class, 'pdp-price')]/text()").extract_first(),
            'description': response.xpath("//div[@id='module_product_detail']/h2/text()").extract_first()
        }
    

这是结果 enter image description here

相关问题