Skip to content

Commit adb13a1

Browse files
nababuddinpre-commit-ci[bot]cclauss
authored
Update instagram_pic.py (#10957)
* Update instagram_pic.py * Update instagram_pic.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update instagram_pic.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update instagram_pic.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update instagram_pic.py * Update instagram_pic.py * Update instagram_pic.py * Update instagram_pic.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update instagram_pic.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update instagram_pic.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update instagram_pic.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fast fail instead of nested ifs and PEP8: Keep try/except blocks small * Update instagram_pic.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
1 parent bad39cd commit adb13a1

File tree

1 file changed

+41
-10
lines changed

1 file changed

+41
-10
lines changed

Diff for: web_programming/instagram_pic.py

+41-10
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,45 @@
33
import requests
44
from bs4 import BeautifulSoup
55

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

Comments
 (0)