簡體   English   中英

我如何使用 scrapy 從 bestbuy 獲取產品的 UPC

[英]How can i get the UPC of a product from bestbuy using scrapy

你好呀

我需要廢棄 bestbuy 我目前正在使用 scrapy 我能夠獲得我需要的大部分數據但是我在嘗試獲取 UPC 所在的規范數據部分時遇到了一些問題。 我能夠獲得功能,但那部分我無法獲取數據。

非常感謝您的幫助這是我的代碼

from scrapy import Spider
from bestbuy_spider.items import BestbuyProductItem
from scrapy import Request
import re
import json


class Bestbuy2Spider(Spider):
    name = 'bestbuy2'
    # allowed_domains = ['https://www.bestbuy.com']
    allowed_domains = ['bestbuy.com']
    # https://www.bestbuy.com/site/searchpage.jsp?cp=1&searchType=search&browsedCategory=pcmcat209400050001&ks=960&sp=-bestsellingsort%20skuidsaas&sc=Global&list=y&usc=All%20Categories&type=page&id=pcat17071&iht=n&nrp=15&seeAll=&st=categoryid%24pcmcat209400050001&qp=carrier_facet%3DCarrier~Verizon
    # start_urls = ['https://www.bestbuy.com/site/laptop-computers/all-laptops/pcmcat138500050001.c?id=pcmcat138500050001']
    start_urls = ['https://www.bestbuy.com/site/searchpage.jsp?id=pcat17071&qp=storepickupstores_facet%3DStore%20Availability%20-%20In%20Store%20Pickup~237&st=%2A']

    def parse(self, response):
        text = response.xpath('//div[@class="left-side"]/span/text()').extract_first()
        _, items_page, total = tuple(map(lambda x: int(x), re.findall('\d+',text)))

        num_pages = total // items_page
        #print('number of pages:', num_pages)


        urls = [
            'https://www.bestbuy.com/site/searchpage.jsp?cp={}&id=pcat17071&qp=storepickupstores_facet%3DStore%20Availability%20-%20In%20Store%20Pickup~237&st=%2A'.format(
                x) for x in range(1, num_pages + 1)]
        for url in urls[:1]:

            # product list page
            yield Request(url=url, callback=self.parse_product_list)

    def parse_product_list(self, response):
        # product list

        rows = response.xpath('//ol[@class="sku-item-list"]/li')
        # print(len(rows))
        # print('=' * 50)
        for row in rows:
            url = row.xpath('.//div[@class="sku-title"]/h4/a/@href').extract_first()
            print(url)
            yield Request(url='https://www.bestbuy.com' + str(url), callback=self.parse_product)
        #'//ul[@Class="thumbnail-list"]//@src'

    def parse_product(self, response):
        price_txt = response.xpath('//div[@class="pricing-price__regular-price"]/text()').extract_first()
        #reg_price = price_txt.replace('Was ', '')




        item = BestbuyProductItem(
            product = response.xpath('//div[@class="sku-title"]/h1/text()').extract_first(),
            #color = response.xpath('li[@class="image selected"]/div/a/@title').extract_first(),
            #skuId = response.xpath('//div[@class="sku product-data"]/span[2]/text()').extract_first(),
            #price = response.xpath('//div[@class="priceView-hero-price priceView-customer-price"]/span[1]/text()').extract_first(),
            #model = response.xpath('//div[@class="model product-data"]/span[2]/text()').extract_first(),
            #main_image = response.xpath('//img[@class="primary-image"]/@src').extract_first(),
            #images = response.xpath('//*[@class="thumbnail-list"]//img/@src').extract(),

            #description = response.xpath('//div[@class="long-description-container body-copy "]//div/text()').extract(),
            #features = response.xpath('//div[@class="list-row"]/p/text()').extract(),
            #regular_price = price_txt,
            Location = response.xpath('//div[@class="fulfillment-fulfillment-summary"]//div/p[1]/span/text()').extract()






        )
        yield item

查看一個產品頁面代碼 ( https://www.bestbuy.com/site/sony-65-class-bravia-xr-x95j-4k-uhd-smart-google-tv/6459306.p?skuId=6459306 ) 我請注意,有一個帶有 gtin13 字段的 json(您要查找的 upc 代碼)。 應該很容易用 json 模塊解析它並得到你需要的東西。

{
 "@context":"http://schema.org/",
 "@type":"Product",
 "name":"Sony - 65\" class BRAVIA XR X95J 4K UHD Smart Google TV",
 "image":"https://pisces.bbystatic.com/image2/BestBuy_US/images/products/6459/6459306_sd.jpg",
 "url":"https://www.bestbuy.com/site/sony-65-class-bravia-xr-x95j-4k-uhd-smart-google-tv/6459306.p?skuId=6459306",
 "description":"Shop Sony 65\" class BRAVIA XR X95J 4K UHD Smart Google TV at Best Buy. Find low everyday prices and buy online for delivery or in-store pick-up. Price Match Guarantee.",
 "sku":"6459306",
 "gtin13":"0027242921818",
 "model":"XR65X95J",
 "width":{
 "@type":"http://schema.org/QuantitativeValue",
  "unitCode":"INH",
  "value":"56.87"
 },
 "color":"Black",
 "brand":{
  "@type":"Brand",
  "name":"Sony"
 },
 "aggregateRating":{
  "@type":"AggregateRating",
  "ratingValue":"4.7",
  "reviewCount":"221"
 },
 "offers":{
   "@type":"AggregateOffer",
   "priceCurrency":"USD",
   "seller":{
     "@type":"Organization",
     "name":"Best Buy"
  },
  "lowPrice":"1184.99",
  "highPrice":"1499.99",
  "offercount":5,
  "offers":[
     {
        "@type":"Offer",
        "priceCurrency":"USD",
        "price":"1499.99",
        "availability":"http://schema.org/InStock",
        "itemCondition":"http://schema.org/NewCondition",
        "description":"New"
     },
     {
        "@type":"Offer",
        "priceCurrency":"USD",
        "price":"1319.99",
        "itemCondition":"http://schema.org/UsedCondition",
        "description":"Open-Box Excellent - Certified"
     },
     {
        "@type":"Offer",
        "priceCurrency":"USD",
        "price":"1274.99",
        "itemCondition":"http://schema.org/UsedCondition",
        "description":"Open-Box Excellent"
     },
     {
        "@type":"Offer",
        "priceCurrency":"USD",
        "price":"1229.99",
        "itemCondition":"http://schema.org/UsedCondition",
        "description":"Open-Box Satisfactory"
     },
     {
        "@type":"Offer",
        "priceCurrency":"USD",
        "price":"1184.99",
        "itemCondition":"http://schema.org/UsedCondition",
        "description":"Open-Box Fair"
     }
  ]
  }
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM