Skip to content

Commit a73dacc

Browse files
Epic-R-Rcclauss
authored andcommitted
Create instagram_pic (TheAlgorithms#3945)
* Create instagram_pic * Update instagram_pic * Update instagram_pic * isort * Update instagram_pic.py Co-authored-by: Christian Clauss <[email protected]>
1 parent 3e43bc8 commit a73dacc

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

web_programming/instagram_pic.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from datetime import datetime
2+
3+
import requests
4+
from bs4 import BeautifulSoup
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
13+
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}.")

0 commit comments

Comments
 (0)