【发布时间】:2021-07-09 20:44:38
【问题描述】:
大家好,我在使用 Beautiful Soup 时发现了一些问题。 我正在尝试抓取 Bangood 的网站,但是,我不知道为什么,我只成功抓取了项目的名称。 使用硒我刮了项目的价格(只有美元不是欧元) 所以我请求你的帮助,如果你知道任何解决这些问题的方法,我会很高兴。
我想抓取姓名、欧元价格、折扣、星星、图片,但我不明白为什么美汤不起作用。
附言。显然我不想要所有的功能,但如果可以的话,我想要美丽的汤给出所有这些问题的原因和一个例子。
现在我正在尝试在此处发布我想要抓取的 html(如果可能的话,在漂亮的汤中)。
谢谢大家!
<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