【问题标题】:Can't scrape Bangood site with beautiful soup and selenium不能用漂亮的汤和硒刮掉班古德网站
【发布时间】:2021-07-09 20:44:38
【问题描述】:

大家好,我在使用 Beautiful Soup 时发现了一些问题。 我正在尝试抓取 Bangood 的网站,但是,我不知道为什么,我只成功抓取了项目的名称。 使用硒我刮了项目的价格(只有美元不是欧元) 所以我请求你的帮助,如果你知道任何解决这些问题的方法,我会很高兴。

我想抓取姓名、欧元价格、折扣、星星、图片,但我不明白为什么美汤不起作用。

附言。显然我不想要所有的功能,但如果可以的话,我想要美丽的汤给出所有这些问题的原因和一个例子。

现在我正在尝试在此处发布我想要抓取的 html(如果可能的话,在漂亮的汤中)。

谢谢大家!

我要抓取的链接 = https://it.banggood.com/ANENG-AN8008-True-RMS-Wave-Output-Digital-Multimeter-AC-DC-Current-Volt-Resistance-Frequency-Capacitance-Test-p-1157985.html?rmmds=flashdeals&cur_warehouse=USA

<span class="main-price" oriprice-range="0-0" oriprice="22.99">19,48€</span>

<strong class="average-num">4.95</strong>



 <img src="https://imgaz1.staticbg.com/thumb/large/oaupload/banggood/images/1B/ED/b3e9fd47-ebb4-479b-bda2-5979c9e03a11.jpg.webp" id="landingImage" data-large="https://imgaz1.staticbg.com/thumb/large/oaupload/banggood/images/1B/ED/b3e9fd47-ebb4-479b-bda2-5979c9e03a11.jpg" dpid="left_largerView_image_180411|product|18101211554" data-src="https://imgaz1.staticbg.com/thumb/large/oaupload/banggood/images/1B/ED/b3e9fd47-ebb4-479b-bda2-5979c9e03a11.jpg" style="height: 100%; transform: translate3d(0px, 0px, 0px);">

这些是我正在使用的功能

这不起作用:

def take_image_bang(soup):   #beautiful soup and json

    img_div = soup.find("div", attrs={"class":'product-image'})

    imgs_str = img_div.img.get('data-large')  # a string in Json format

    # convert to a dictionary
    imgs_dict = json.loads(imgs_str)
    print(imgs_dict)
    #each key in the dictionary is a link of an image, and the value shows the size (print all the dictionay to inspect)
    #num_element = 0 
    #first_link = list(imgs_dict.keys())[num_element]
    return imgs_dict

这些有效(但函数 get_price 仅适用于美元而不是欧元):

def get_title_bang(soup): #beautiful soup
     
    try:
        # Outer Tag Object
        title = soup.find("span", attrs={"class":'product-title-text'})
 
        # Inner NavigableString Object
        title_value = title.string
 
        # Title as a string value
        title_string = title_value.strip()
 
        # # Printing types of values for efficient understanding
        # print(type(title))
        # print(type(title_value))
        # print(type(title_string))
        # print()
 
    except AttributeError:
        title_string = ""   
 
    return title_string

def get_Bangood_price(driver):  #selenium

    c = CurrencyConverter()

    prices = driver.find_elements_by_class_name('main-price')
    
    
    for price in prices:

        price = price.text.replace("US$","")
        priceZ = float(price)
        
        
        price_EUR = c.convert(priceZ, 'USD', 'EUR')

    return price_EUR
    

    

    

【问题讨论】:

标签: python selenium web-scraping beautifulsoup


【解决方案1】:

如果您想更改欧元网址中的价格,您可以根据网页进行设置

import requests
from bs4 import BeautifulSoup
headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"}
res=requests.get("https://it.banggood.com/ANENG-AN8008-True-RMS-Wave-Output-Digital-Multimeter-AC-DC-Current-Volt-Resistance-Frequency-Capacitance-Test-p-1157985.html?rmmds=flashdeals&cur_warehouse=USA&DCC=IT&currency=EUR",headers=headers)
soup=BeautifulSoup(res.text,"lxml")

查找标题:

main_div=soup.find("div",class_="product-info")
title=main_div.find("h1",class_="product-title").get_text(strip=True)
print(title)

输出:

ANENG AN8008 Vero RMS Digitale Multimetri Tester di AC DC Corrente Tensione Resistenza Frenquenza CapacitàCOD

查找评论:

star=[i.get_text(strip=True) for i in main_div.find("div",class_="product-reviewer").find_all("dd")]
star

输出:

['5 Stella2618 (95.8%)',
 '4 Stella105 (3.8%)',
 '3 Stella9 (0.3%)',
 '2 Stella0 (0.0%)',
 '1 Stella2 (0.1%)']

查找价格和其他您可以从script 标签使用json 加载它!

data=soup.find("script", attrs={"type":"application/ld+json"}).string.strip().strip(";")
import json
main_data=json.loads(data)

从中寻找价值:

price=main_data['offers']['priceCurrency']+" "+main_data['offers']['price']
image=main_data['image']
print(price,image)

输出:

EUR 19.48 https://imgaz3.staticbg.com/thumb/view/oaupload/banggood/images/1B/ED/b3e9fd47-ebb4-479b-bda2-5979c9e03a11.jpg

在价格动态更新时查找折扣价,您可以 使用 xhr 链接调用并从中查找数据!这里是

url 使用发布请求!

【讨论】:

    【解决方案2】:

    要以欧元抓取数据,您需要更改链接地址并将其添加到链接末尾:

    • 欧元加:&currency=EUR
    • 对于美元添加:&currency=USD

    对于欧元,链接应该是: https://it.banggood.com/ANENG-AN8008-True-RMS-Wave-Output-Digital-Multimeter-AC-DC-Current-Volt-Resistance-Frequency-Capacitance-Test-p-1157985.html?rmmds=flashdeals&cur_warehouse=USA&currency=EUR

    再举个例子:如果你想换货换仓库:

    • 对于 CN 更改:cur_warehouse=CN
    • 对于美国更改:cur_warehouse=USA
    • 对于 PL 更改:cur_warehouse=PL

    这些是根据输入改变网页的 URL 的动态变量。

    在此之后,您的第二种方法应该可以正常工作。快乐!!!

    【讨论】:

      猜你喜欢
      • 2017-08-15
      • 1970-01-01
      • 2020-08-09
      • 1970-01-01
      • 2021-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-23
      相关资源
      最近更新 更多