Created
November 13, 2020 04:51
-
-
Save thisismattmiller/3d94aedb9d58f016037e7387b72ff3a8 to your computer and use it in GitHub Desktop.
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 requests | |
import json | |
btoken = 'YOUR_TOKEN_HERERERRERER' | |
url = 'https://api.twitter.com/2/tweets/search/recent' | |
headers = {"Authorization": f"Bearer {btoken}"} | |
all_data = [] | |
payload = { | |
'query' : '"cultural heritage"', | |
'max_results': 100, | |
'expansions' : 'attachments.poll_ids,attachments.media_keys,author_id,entities.mentions.username,geo.place_id,in_reply_to_user_id,referenced_tweets.id,referenced_tweets.id.author_id', | |
'media.fields' : 'duration_ms,height,media_key,preview_image_url,type,url,width,public_metrics', | |
'tweet.fields' : 'attachments,author_id,context_annotations,conversation_id,created_at,entities,geo,id,in_reply_to_user_id,lang,public_metrics,possibly_sensitive,referenced_tweets,source,text,withheld' | |
} | |
r = requests.get(url,params=payload, headers=headers) | |
data = json.loads(r.text) | |
all_data = all_data + data['data'] | |
next_token = data['meta']['next_token'] | |
for number in range(1,5): | |
print('on request #',number) | |
payload = { | |
'query' : '"cultural heritage"', | |
'next_token' : next_token | |
} | |
r = requests.get(url,params=payload, headers=headers) | |
all_data = all_data + data['data'] | |
next_token = data['meta']['next_token'] | |
json.dump(all_data,open('twitter_data_tmp.json','w'),indent=2) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment