Skip to content

Instantly share code, notes, and snippets.

@Allwin12
Created May 17, 2020 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Allwin12/a9f547bc0b4ee75874a5ba33a427856d to your computer and use it in GitHub Desktop.
Save Allwin12/a9f547bc0b4ee75874a5ba33a427856d to your computer and use it in GitHub Desktop.
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