【问题标题】:Scrapy xpath selector doesnt select all the HTML tagsScrapy xpath 选择器不会选择所有 HTML 标签
【发布时间】:2018-08-01 00:30:27
【问题描述】:

我正在尝试使用 Scrapy python 库抓取 https://www.walmart.com/search/?query=ps3&cat_id=0 上的所有产品名称。

这是我的解析函数

    def parseWalmart(self,response):

        print("INSIDE PARSE WALMART")

        for product in response.xpath('//div[@id="searchProductResult"]/div[@class="search-result-listview-items"]//div[starts-with(@data-tl-id,"ProductTileListView-")]'):

            print(product)
            product_name = product.xpath('.//div[contains(@class,"search-result-product-title listview")]//a//span//text()').extract()
            product_page = product.xpath('.//div[contains(@class,"search-result-product-title listview")]//a/@href').extract()

            product_name=" ".join(product_name)
            print(product_name)
            print("-------------------------------------")

这是我的scrapy请求

    yield scrapy.Request(url=i, callback=self.parseWalmart, headers = {"User-Agent":"Mozilla/5.0"})

但是,我只能抓取 4 个产品,而实际上有十几个产品。我不明白为什么。这是我抓取的 4 款产品

<Selector xpath='//div[@id="searchProductResult"]/div[@class="search-result-listview-items"]//div[starts-with(@data-tl-id,"ProductTileListView-")]' data='<div data-tl-id="ProductTileListView-0">'>
ABLEGRID Wireless Bluetooth Game Controller for Sony  PS3  Black
-------------------------------------
<Selector xpath='//div[@id="searchProductResult"]/div[@class="search-result-listview-items"]//div[starts-with(@data-tl-id,"ProductTileListView-")]' data='<div data-tl-id="ProductTileListView-1">'>
Arsenal Gaming  PS3  Wired Controller, Black
-------------------------------------
<Selector xpath='//div[@id="searchProductResult"]/div[@class="search-result-listview-items"]//div[starts-with(@data-tl-id,"ProductTileListView-")]' data='<div data-tl-id="ProductTileListView-2">'>
Refurbished Sony PlayStation 3 Slim 320 GB Charcoal Black Console
-------------------------------------
<Selector xpath='//div[@id="searchProductResult"]/div[@class="search-result-listview-items"]//div[starts-with(@data-tl-id,"ProductTileListView-")]' data='<div data-tl-id="ProductTileListView-3">'>
Sonic's Ultimate Genesis Collection ( PS3 )
-------------------------------------

【问题讨论】:

    标签: html xpath web-scraping scrapy


    【解决方案1】:

    因为最初在 DOM 中只有 4 个以 'ProductTileListView-' 开头的 div。但是,您可以在页面的脚本中找到所有产品信息。

    这是我获取产品所有信息的方式

    import re import json data = re.findall("\"items\":(.+?),\"secondaryItems\"", response.body.decode("utf-8"), re.S) products_json = json.loads(data[0]) len(ls) # return 20 请注意,products 数组以“items”开头,以“secondaryItems”结尾。

    一种产品的结构 { "productId": "2H53I08Z1K78", "usItemId": "23422902", "productType": "REGULAR", "title": "Watch Dogs (<mark>PS3</mark>)", .... "imageUrl": "https://i5.walmartimages.com/asr/70aecbb1-5dbf-4a64-a86d-134a8fc7edee_2.59805d79db07665c20cc4e4fadc35743.jpeg?odnHeight=180&odnWidth=180&odnBg=ffffff", "productPageUrl": "/ip/Watch-Dogs-PS3/23422902", "upc": "0000888834804", }

    【讨论】:

    • 我在walmart.com/search/?query=ps3&cat_id=0的GET请求中没有看到json“项目”
    • @darbulix 我相信截图说明了你需要的一切。
    • 我是说我没有得到你在开发工具上得到的东西
    • 我知道如何使用检查器,但是当我进入网络时,我并没有得到你在响应下得到的确切信息。您能否介绍一下您为实现这一目标所采取的步骤?
    • 找到服务器返回的正确文档。如果文档为空,则强制浏览器重新加载页面中的所有资源。您可能想要漂亮地打印它。
    猜你喜欢
    • 2019-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-23
    • 2016-03-12
    • 1970-01-01
    • 2017-04-25
    相关资源
    最近更新 更多