Skip to content

Instantly share code, notes, and snippets.

@tikseniia
Created February 4, 2019 23:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tikseniia/4888d313652637517538d4b7ad6153ba to your computer and use it in GitHub Desktop.
Save tikseniia/4888d313652637517538d4b7ad6153ba to your computer and use it in GitHub Desktop.
Get Instagram posts for tag
import requests
import time
import json
arr = []
end_cursor = '' # empty for the 1st page
tag = 'russia' # your tag
page_count = 5 # desired number of pages
for i in range(0, page_count):
url = "https://www.instagram.com/explore/tags/{0}/?__a=1&max_id={1}".format(tag, end_cursor)
r = requests.get(url)
data = json.loads(r.text)
end_cursor = data['graphql']['hashtag']['edge_hashtag_to_media']['page_info']['end_cursor'] # value for the next page
edges = data['graphql']['hashtag']['edge_hashtag_to_media']['edges'] # list with posts
for item in edges:
arr.append(item['node'])
time.sleep(2) # insurence to not reach a time limit
print(end_cursor) # save this to restart parsing with the next page
with open('posts.json', 'w') as outfile:
json.dump(arr, outfile) # save to json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment