在使用Python / Beautyfulsoup抓取产品图像时,我做错了什么?



我正在学习如何使用python进行抓取。因此,我在这个网站上使用python代码:https://www.freecodecamp.org/news/scraping-ecommerce-website-with-python/

这一切都很好,但我也想从这个页面上抓取产品图像https://www.thewhiskyexchange.com/p/29388/hibiki-harmony

代码如下:

<div class="product-main__image-container">
<img src="https://img.thewhiskyexchange.com/900/japan_hib11.jpg" alt="Hibiki Harmony" class="product-main__image" width="3" height="4" />
</div>

我的问题是:我该如何用Python和Beautysol汤来抓取这张图片。我尝试了不同的方法,但都不起作用。以下是我的无效代码:

try:
image = hun.find("img", {"class": "product-main__image"}).text.replace('n', "")
except:
image = None

如果你想抓取图像url,那么使用bs4是很容易的,只有这样你才能尝试下一个例子。

import requests
from bs4 import BeautifulSoup
url = 'https://www.thewhiskyexchange.com/p/29388/hibiki-harmony'
soup=BeautifulSoup(requests.get(url).content, "html.parser")
image_url = soup.find("img", {"class": "product-main__image"}).get('src')
print(image_url)

输出:

https://img.thewhiskyexchange.com/900/japan_hib11.jpg

相关内容

最新更新