Created
May 17, 2020 13:45
-
-
Save Allwin12/a9f547bc0b4ee75874a5ba33a427856d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import urllib.parse | |
from PIL import Image | |
from io import BytesIO | |
from bs4 import BeautifulSoup | |
import re, sys | |
def image(username): | |
url = "https://www.instagram.com/{}/".format(username) | |
session = requests.session() | |
# header parameter is used to resolve the bad gateway 502 error | |
# html = session.get(url, headers={'User-Agent': 'Mozilla/5.0'}).text | |
# opening the URL and parsing it into BeautifulSoup | |
try: | |
html = session.get(url, headers={'User-Agent': 'Mozilla/5.0'}).text | |
except: | |
print("Page not found or No internet connection") | |
sys.exit() | |
soup = BeautifulSoup(html, 'html.parser') | |
tags = soup.find_all('body') | |
# using regualr expression to extract the image URL | |
profile_pic_url_hd = re.findall(r"profile_pic_url_hd\":\"([\S]+?)\"", str(tags[0]))[0].replace(r'\u0026', '&') | |
response = session.get(profile_pic_url_hd, headers={'User-Agent': 'Mozilla/5.0'}).content | |
img = Image.open(BytesIO(response)) | |
img.show() | |
img.save("{}.png".format(username)) | |
image('selenagomez') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment