Created
February 4, 2019 23:50
-
-
Save tikseniia/4888d313652637517538d4b7ad6153ba to your computer and use it in GitHub Desktop.
Get Instagram posts for tag
This file contains hidden or 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 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