|
3 | 3 | import requests
|
4 | 4 | from bs4 import BeautifulSoup
|
5 | 5 |
|
6 |
| -if __name__ == "__main__": |
7 |
| - url = input("Enter image url: ").strip() |
8 |
| - print(f"Downloading image from {url} ...") |
9 |
| - soup = BeautifulSoup(requests.get(url).content, "html.parser") |
10 |
| - # The image URL is in the content field of the first meta tag with property og:image |
11 |
| - image_url = soup.find("meta", {"property": "og:image"})["content"] |
12 |
| - image_data = requests.get(image_url).content |
| 6 | + |
| 7 | +def download_image(url: str) -> str: |
| 8 | + """ |
| 9 | + Download an image from a given URL by scraping the 'og:image' meta tag. |
| 10 | +
|
| 11 | + Parameters: |
| 12 | + url: The URL to scrape. |
| 13 | +
|
| 14 | + Returns: |
| 15 | + A message indicating the result of the operation. |
| 16 | + """ |
| 17 | + try: |
| 18 | + response = requests.get(url) |
| 19 | + response.raise_for_status() |
| 20 | + except requests.exceptions.RequestException as e: |
| 21 | + return f"An error occurred during the HTTP request to {url}: {e!r}" |
| 22 | + |
| 23 | + soup = BeautifulSoup(response.text, "html.parser") |
| 24 | + image_meta_tag = soup.find("meta", {"property": "og:image"}) |
| 25 | + if not image_meta_tag: |
| 26 | + return "No meta tag with property 'og:image' was found." |
| 27 | + |
| 28 | + image_url = image_meta_tag.get("content") |
| 29 | + if not image_url: |
| 30 | + return f"Image URL not found in meta tag {image_meta_tag}." |
| 31 | + |
| 32 | + try: |
| 33 | + image_data = requests.get(image_url).content |
| 34 | + except requests.exceptions.RequestException as e: |
| 35 | + return f"An error occurred during the HTTP request to {image_url}: {e!r}" |
| 36 | + if not image_data: |
| 37 | + return f"Failed to download the image from {image_url}." |
| 38 | + |
13 | 39 | file_name = f"{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg"
|
14 |
| - with open(file_name, "wb") as fp: |
15 |
| - fp.write(image_data) |
16 |
| - print(f"Done. Image saved to disk as {file_name}.") |
| 40 | + with open(file_name, "wb") as out_file: |
| 41 | + out_file.write(image_data) |
| 42 | + return f"Image downloaded and saved in the file {file_name}" |
| 43 | + |
| 44 | + |
| 45 | +if __name__ == "__main__": |
| 46 | + url = input("Enter image URL: ").strip() or "https://www.instagram.com" |
| 47 | + print(f"download_image({url}): {download_image(url)}") |
0 commit comments