我想用Twitter API通过关键字抓取Twitter。我正在使用Twitter搜索API。
query = 'football'
tweet_fields = "author_id,created_at,text,public_metrics,possibly_sensitive,source,lang"
max_results = "50"
#define search twitter function
headers = {"Authorization": "Bearer {}".format(BEARER_TOKEN)}
url = "https://api.twitter.com/2/tweets/search/recent?query={}&tweet.fields={}&max_results={}".format(query, tweet_fields, max_results)
response = requests.request("GET", url, headers=headers)
status_code = response.status_code
print("Response Status Code:", status_code)
if response.status_code != 200:
raise Exception(response.status_code, response.text)
else:
pass
#print(response.json())
twitter_search_data = response.json()['data']
twitter_response = []
for data in twitter_search_data:
print(data)
author_usernameauthor_id
我已尝试将此添加到我的API链接,但没有得到这些结果:
expansions=author_id&user.fields={}
user_fields = "description,username"
url = "https://api.twitter.com/2/tweets/search/recent?query={}&tweet.fields={}&expansions=author_id&user.fields={}&max_results={}".format(query, tweet_fields, user_fields, max_results)
这是一个示例结果:
{'possibly_sensitive': False, 'source': 'Twitter for Android', 'lang': 'en', 'public_metrics': {'retweet_count': 1, 'reply_count': 0, 'like_count': 0, 'quote_count': 0}, 'created_at': '2021-10-05T12:23:05.000Z', 'id': '1445363916457005058', 'text': 'RT @COiNSTANTIN1: @MEXC_Global @PolkaExOfficial Check out @MiniFootballBsc We are bringing together the football and crypto community.\n⚽️Fa…', 'author_id': '1444275133854715912'}
有没有办法向我的Twitter API添加一些东西,这样我就可以获得:1.author用户名2.author姓名3.number作者的追随者4.number作者的以下内容