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

How to call historical Tweets with metadata using Twitter API Python wrapper `searchtweets-v2`

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

当我之前通过具有高级访问权限的帐户使用 API 时,返回的推文带有大量元数据。但是,当我尝试使用以下脚本时,基于 searchtweets-v2 webpage 中的代码,它只有 returns 推文 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's code, published here,效果很好。