mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
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 <cclauss@me.com>
This commit is contained in:
parent
bad39cd154
commit
adb13a1063
|
@ -3,14 +3,45 @@ from datetime import datetime
|
||||||
import requests
|
import requests
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
url = input("Enter image url: ").strip()
|
def download_image(url: str) -> str:
|
||||||
print(f"Downloading image from {url} ...")
|
"""
|
||||||
soup = BeautifulSoup(requests.get(url).content, "html.parser")
|
Download an image from a given URL by scraping the 'og:image' meta tag.
|
||||||
# The image URL is in the content field of the first meta tag with property og:image
|
|
||||||
image_url = soup.find("meta", {"property": "og:image"})["content"]
|
Parameters:
|
||||||
image_data = requests.get(image_url).content
|
url: The URL to scrape.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
A message indicating the result of the operation.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
response = requests.get(url)
|
||||||
|
response.raise_for_status()
|
||||||
|
except requests.exceptions.RequestException as e:
|
||||||
|
return f"An error occurred during the HTTP request to {url}: {e!r}"
|
||||||
|
|
||||||
|
soup = BeautifulSoup(response.text, "html.parser")
|
||||||
|
image_meta_tag = soup.find("meta", {"property": "og:image"})
|
||||||
|
if not image_meta_tag:
|
||||||
|
return "No meta tag with property 'og:image' was found."
|
||||||
|
|
||||||
|
image_url = image_meta_tag.get("content")
|
||||||
|
if not image_url:
|
||||||
|
return f"Image URL not found in meta tag {image_meta_tag}."
|
||||||
|
|
||||||
|
try:
|
||||||
|
image_data = requests.get(image_url).content
|
||||||
|
except requests.exceptions.RequestException as e:
|
||||||
|
return f"An error occurred during the HTTP request to {image_url}: {e!r}"
|
||||||
|
if not image_data:
|
||||||
|
return f"Failed to download the image from {image_url}."
|
||||||
|
|
||||||
file_name = f"{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg"
|
file_name = f"{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg"
|
||||||
with open(file_name, "wb") as fp:
|
with open(file_name, "wb") as out_file:
|
||||||
fp.write(image_data)
|
out_file.write(image_data)
|
||||||
print(f"Done. Image saved to disk as {file_name}.")
|
return f"Image downloaded and saved in the file {file_name}"
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
url = input("Enter image URL: ").strip() or "https://www.instagram.com"
|
||||||
|
print(f"download_image({url}): {download_image(url)}")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user