Last active
August 1, 2021 19:56
-
-
Save moosetraveller/c15850e23b1869d17bcdbfcfcb891299 to your computer and use it in GitHub Desktop.
Download Instagram Images with Python and instagram-scraper
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 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