Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<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

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading