[Solved] How to scrape simple image from webpage


In your code, the image_url gives the directory of the image where it stored on the hosting service. You need to append the domain name to the image_url variable and use the requests library to download it.

Use the following code and it will work.

import bs4
import requests

url = "https://parts.bmwmonterey.com/a/BMW_2004_330i-Sedan/_52014_5798240/Cooling-System-Water-Hoses/17_0215.html"

resp = requests.get(url)

soup = bs4.BeautifulSoup(resp.text, "html.parser")

img = soup.find('img')

image = img["src"]

img_url = "https://parts.bmwmonterey.com" + str(image)

r = requests.get(img_url)

with open("image.jpg","wb") as f:
    f.write(r.content)


1

solved How to scrape simple image from webpage