What I'm doing wrong when scraping a productimage with Python / Beautyfulsoup?

Question:

I’m trying to learn howto scrape with python. Therefore I’m using the python code on this site: https://www.freecodecamp.org/news/scraping-ecommerce-website-with-python/

This all works just fine but I also want to scrape the product image from this page
https://www.thewhiskyexchange.com/p/29388/hibiki-harmony

The code is the following:

<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>

My question is: how can I scrape this image with Python and Beautysolsoup.
I tried different things but none of them are working.
Hereby my not working code:

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

||

Answers:

If you want to scrape the image url then it’s possible with bs4 easily and only then you can try the next example.

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)

Output:

https://img.thewhiskyexchange.com/900/japan_hib11.jpg
Answered By: Fazlul
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.