这个网站的价格如何呈现?

时间:2019-04-26 10:15:33

标签: python python-3.x scrapy

我需要一个蜘蛛来抓取这个网站():

我希望搜寻器下载此页面的产品信息(https://search.suning.com/iphone/),包括价格,但我无法直接从此页面获得该信息。该页面的价格似乎是异步呈现的,但是我找不到它的执行方式。有人能帮我吗?谢谢。

我在Chrome Developer工具中检查了“网络”面板,但找不到它。

# just example , not done
class SuningSpider(scrapy.Spider):
    name = "sn"
    keyWord = "笔"
    prefix = "https://"

    def start_requests(self):
        yield scrapy.Request(url="https://search.suning.com/%s/" % self.keyWord, callback=self.parse)

    def parse(self, response):
        logging.error(response.text)
        selector = scrapy.Selector(response)


        productLists = selector.xpath("//div[@id='product-list']/ul/li")

        for p in productLists:
            deatailUrl = p.css("div > div > div.res-info > div.title-selling-point > a::attr(href)")
            # price = p.css("div > div > div.res-info > div.price-box > span > i:nth-child(1)::text")
            s.xpath('//*[@id="0070130312-164968740"]/div/div/div[2]/div[1]/span/i[2]')
            # //div[contains(@class,'foo')]
            fullUrl = self.prefix + deatailUrl
            yield scrapy.Request(url=deatailUrl, callback=)


    def crawlDetailPage(self, response):
        selector = scrapy.Selector(response)
        price = selector.xpath('//*[@id="mainPrice"]/dl[1]/dd/span')

    def finished(self, response):
        item = DataItem()
        item["url"] = response.url
        selector = scrapy.Selector(response)

        price = selector.xpath("")

1 个答案:

答案 0 :(得分:2)

价格异步呈现到JavaScript回调(jsonp)中,您将看到这些请求已发布到ds.suning.com。不过,您可以通过更改请求的网址来获取纯json。当您向下滚动页面时会调用它们,因此您将需要使用产品urls动态构建这些ids

这为您提供页面上前5个元素的价格:

from bs4 import BeautifulSoup
import requests
import json

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0'}

with requests.Session() as s:

      baseurl = 'https://ds.suning.com/ds/generalForTile/000000010606656239_,000000000690128135_,000000010606649857_,000000010597840415_,000000010597840391_-010-2-0000000000-1--ds0000000002391.json'

      url_fetch = s.get(baseurl, headers=headers).json()

      print([rs['price'] for rs in url_fetch['rs']])

      #['5588.00', '5988.00', '7188.00', '7688.00', '5188.00']