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