如何使用Twitter API Python包装器"searchtweets-v2'"调用带有元数据



我是一名研究人员,可以访问Twitter的新学术产品轨道。我正在试验Python包装器searchtweets-v2(在这里找到(来尝试调用Tweets。

当我之前通过具有高级访问权限的帐户使用API时,推文会返回大量元数据。然而,当我尝试使用以下脚本时,基于searchtweets-v2网页中的代码,它只返回一个推特ID和文本:

代码:

# Import packages
from searchtweets import ResultStream, gen_request_parameters, load_credentials, collect_results
# Load in previously saved credentials
search_args = load_credentials("~/.twitter_keys.yaml",
yaml_key="search_tweets_v2",
env_overwrite=False)
# Query inputs
search_terms = "snow"
add_terms    = ' lang:en place_country:GB'
max_results  = 100
start_time   = '"2020-02-01T00:00:00Z"'
end_time     = '"2020-02-08T00:00:00Z"'
# Build queries
tweet_full_query  = '{"query":' + '"' + search_terms.replace('"', '\"') + add_terms.replace('"', '\"') + '"' + ',"max_results":' + str(max_results) + 
',"start_time":' + str(start_time) + ',"end_time":' + str(end_time) + '}'
# Call Tweets using 'collect_results'
tweets = collect_results(tweet_full_query,
max_tweets=100,
result_stream_args=academic_search_args)
# View results
tweets

输出(仅限前6条推文(

[{'data': [{'id': '1225926868592885765',
'text': '@JoeSalter89 I have, I recall a lot of meat being attacked with swords. Snow would’ve said it was an excellent victory it’s just a shame Wellington was Irish and his wife was a drug-addled cricket. Casual xenophobia and misogyny are his hallmark . I’m quoting his exact words as I imagine them'},
{'id': '1225926288583643141',
'text': '@ElfynEvans @TGR_WRC @CoedyBreninFP @RallySweden @TweeksCycles @bikeonscott Best of luck on the snow @ElfynEvans ! 👍 Now you just need to maintain @TGR_WRC ‘s winning record  at @RallySweden 😁👍....no pressure eh! 😉😁'},
{'id': '1225926214608728065',
'text': '@MissElks What the fuck bro why so much snow'},
{'id': '1225926091690446849',
'text': 'Full Snow Moon in Leo on February 8/9 at 20 degrees will bring forth everything that lies in our heart center when it comes to our creativity, our love lives, and our ego. We will be thinking long and hard about our… https://url'},
{'id': '1225924864147677186',
'text': '@JoeSalter89 Snow is a bully. He recently disinterred the bones of Nelson to ask him why the hell he dragged Villeneuve across the Atlantic and back instead of fucking him over in Brittany. He’s drunk on power. And meths.'},
{'id': '1225922095168839680',
'text': '@leftmidfielder @Jay29ers @museumofjerseys It is true. Your atmospheric short story on the disused rail line in the snow hinted at a lot more if you set your mind to it.'}...

我也尝试过使用ResultStream函数,但得到了相同的输出:

# Call tweets using 'ResultStream'
rs = ResultStream(request_parameters=tweet_full_query,
max_results=500,
max_pages=1,
**academic_search_args)
tweets = list(rs.stream())
# View results
tweets

我尝试过探索这些对象,但它们似乎没有任何我能找到的额外元数据。

有人对如何使用新的学术产品轨道使用元数据调用历史推文有什么建议吗?理想情况下,这将通过searchtweets-v2。不过,也欢迎其他建议

我找到了一个变通方法或排序。我没有意识到,你现在必须添加额外的查询参数才能获得更多关于每条推特的基本信息。这里总结了这些查询参数。

我没能使searchtweets-v2适应这项任务。然而,我发现Andrew Edward在这里发布的代码运行得非常好。

最新更新