如何使用 Twitter API Python 包装器 `searchtweets-v2` 调用带有元数据的历史推文

问题描述

我是一名研究员,可以访问 Twitter 的新学术产品轨道。我正在尝试使用 Python 包装器 searchtweets-v2(找到 here)来尝试调用推文。

当我之前通过具有高级访问权限的帐户使用 API 时,推文返回了大量元数据。但是,当我尝试使用以下脚本时,根据 searchtweets-v2 webpage 中的代码,它只返回推文 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。但是,也欢迎其他建议!

解决方法

我找到了一种解决方法或排序方法。我没有意识到您现在必须添加额外的查询参数来接收有关每条推文的更多基本信息。这些查询参数汇总here

我没能让 searchtweets-v2 适应这项任务。但是,我发现发布了 Andrew Edward'shere 代码效果很好。