Skip to content

Instantly share code, notes, and snippets.

@moosetraveller
Last active August 1, 2021 19:56
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 moosetraveller/c15850e23b1869d17bcdbfcfcb891299 to your computer and use it in GitHub Desktop.
Save moosetraveller/c15850e23b1869d17bcdbfcfcb891299 to your computer and use it in GitHub Desktop.
Download Instagram Images with Python and instagram-scraper
import os
import json
import requests
import shutil
# prerequisites:
# pip install instagram-scraper
# pip install requests
# do only use to download your own images
# before running this script, please consult Instagram's term of use
# see also:
# - https://github.com/arc298/instagram-scraper
# - https://help.instagram.com/581066165581870
username = "your_user_name"
os.system(f"instagram-scraper " + username + " --media-metadata --media-types none")
with open(f"{username}/{username}.json") as json_file:
data = json.load(json_file)
images = [record for record in data["GraphImages"] if record["__typename"] == "GraphImage"]
for image in images:
filename = f"{image['id']}.jpg"
image_folder = f"{username}/images"
url = image["display_url"]
os.makedirs(image_folder, exist_ok=True)
req = requests.get(url, stream=True)
if req.status_code == 200:
with open(f"{image_folder}/{filename}", "wb") as file:
req.raw.decode_content = True
shutil.copyfileobj(req.raw, file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment