diff --git a/images_scrapper.py b/images_scrapper.py index 1f3fd07..468065a 100644 --- a/images_scrapper.py +++ b/images_scrapper.py @@ -1,7 +1,21 @@ import requests from bs4 import BeautifulSoup import scrapper -import time +import os + +def download_image(image_url, file_dir): + response = requests.get(image_url) + + if response.status_code == 200: + directory = os.path.dirname(file_dir) + if not os.path.exists(directory): + os.makedirs(directory) + + with open(file_dir, "wb") as fp: + fp.write(response.content) + print("Image downloaded successfully.") + else: + print(f"Failed to download the image. Status code: {response.status_code}") titles, engines = scrapper() for engine in engines: @@ -26,4 +40,4 @@ if soup.find("table", {"class" : "infobox"}).find("td", {"class": "infobox-image"}).find("img").get("srcset"): if len(soup.find("table", {"class" : "infobox"}).find("td", {"class": "infobox-image"}).find("img").get("srcset").split()) > 2: pic_url = "https:" + str(soup.find("table", {"class" : "infobox"}).find("td", {"class": "infobox-image"}).find("img").get("srcset").split()[2]) - print(pic_url) + download_image(pic_url, f"/images/{engine_name}.jpg")