蘇寧易購網址爬蟲爬取商品資訊及圖片

專注的阿熊發表於2021-10-12

# -*- coding: utf-8 -*-

import scrapy

import time

import re

import json

import jsonpath

import urllib.parse

from Suning.items import SuningItem

class SuningSpider(scrapy.Spider):

     name = 'suning'

     allowed_domains = ['search.suning.com/']

     keyword = input(" 請輸入商品: ")

     temp_data = urllib.parse.quote(keyword)

     temp_url = "{}/"

     val_url = temp_url.format(temp_data)

     start_urls = [val_url]

     def __init__(self, name=None, **kwargs):

         super().__init__(name=None, **kwargs)

         self.page_num = 0

     def parse(self, response):

         # content = response.body.decode("utf-8")

         # with open("./file/ 蘇寧 .html", "w", encoding="utf-8") as file:

         #     file.write(content)

         li_elements = response.xpath("//div[@id='product-list']/ul[@class='general clearfix']/li")

         # print(len(li_elements))

         for li_element in li_elements:

             title_elements = li_element.xpath(

                 ".//div[@class='res-info']/div[@class='title-selling-point']/a//text()").extract()

             title_list = []

             for temp_title in title_elements:

                 temp_title = re.sub(r"\s", "", temp_title)

                 if len(temp_title) > 0:

                     temp_title = temp_title.replace(",", " ")

                     title_list.append(temp_title)

             title = "-".join(title_list)

             store_name = li_element.xpath(

                 ".//div[@class='res-info']/div[@class='store-stock']/a/@title").extract_first()

             # print(store_name)

             # print(title)

             temp_image_url = li_element.xpath(

                 ".//div[@class='img-block']/a[@class='sellPoint']/img/@src").extract_first()

             image_url = "https:" + temp_image_url

             # print(image_url)

             temp_product_url = li_element.xpath(

                 ".//div[@class='img-block']/a[@class='sellPoint']/@href").extract_first()

             src_args = re.findall(r"com/(.*?).html", temp_product_url)[0]

             key0 = src_args.split("/")[0]

             key1 = src_args.split("/")[-1]

             price_src =外匯跟單gendan5.com " + key1 + "_0000000" + key1 + "_" + key0 + "_190_755_7550199_500353_1000051_9051_10346_Z001___R9006372_0.91_1___00031F072____0___750.0_2__500363_500519__.html?callback=pcData&_=1630468559926"

             # price_src = " + key1 + "_0000000" + key1 + "_" + key0 + "_250_029_0290199_20089_1000257_9254_12006_Z001___R1901001_0.5_0___000060864___.html?callback=pcData&_=1630466740130"

             # print(price_src)

             item = {"title": title, "store_name": store_name, "image_url": image_url}

             yield scrapy.Request(price_src, callback=self.get_price, dont_filter=True, meta=item)


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2795581/,如需轉載,請註明出處,否則將追究法律責任。

相關文章