使用scrapy从无限滚动页面中刮取数据?

2024-09-29 23:33:06 发布

您现在位置:Python中文网/ 问答频道 /正文

向下滚动时的响应url为:

https://dir.dummymart.com/impcat/next?mcatId=20467&prod_serv=P&mcatName=laser-cutting-machines&srt=97&end=116&ims_flag=&cityID=&fcilp=0&pr=0&pg=5&frsc=28

响应数据在ajax中的格式如下:

{"page_var":"<div id=\"page_variables................

我的蜘蛛代码是:

import scrapy


class DummymartSpider(scrapy.Spider):
    name = 'dummymart'
    allowed_domains = ['dir.dummymart.com']
    start_urls = ['https://dir.dummymart.com/impcat/industrial-machinery.html',
            
                ]

    def parse(self, response):
        Company = response.xpath('//*[@class="lcname"]/text()').extract()
        product = response.xpath('//*[@class="pnm ldf cur"]/text()').extract()
        address = response.xpath('//*[@class="clg"]/text()').extract()
        phone = response.xpath('//*[@class="ls_co phn bo"]/text()').extract()

        for item in zip(Company,product,address,phone):
            scraped_info = {
                'Company':item[0],
                'Product': item[1],
                'Address':item[2],
                'phone':item[3]

            }
            yield scraped_info

如何在向下滚动页面后刮取加载的数据?此外,数据是ajax格式的,而不是json格式的。谢谢


Tags: 数据texthttpscomresponse格式dirphone
1条回答
网友
1楼 · 发布于 2024-09-29 23:33:06

您可以采用两种方法:- 1.使用像Selenium这样的无头浏览器,或者如果您在Scrapy中工作,那么您也可以尝试Splash,它允许您通过Scrapy运行js函数。 2.只需将页面滚动到要删除数据的位置,将该页面下载为HTML,然后运行普通代码

第二种方法是小手册,但如果你只想废弃几页,那么我建议你只使用后一种方法

相关问题 更多 >

    热门问题