为了构建这样的东西,你可以使用推文注释,它们是 Twitter API v2 的一部分(官方文档链接 https://developer.twitter.com/en/docs/twitter-api/annotations/overview)。这些注释在 Twitter 内部用于支持主题功能,但它们可能并不总是直接映射。
请注意,并非所有注释都可以通过 API 获得,例如之前的回答中提到过 https://stackoverflow.com/questions/74094850/retrieve-viral-tweets-topic-from-twitter-api/74109739#74109739.
让我们以您的例子为例 - 您正在查看显示您正在关注的内容的主题页面。我还关注增强现实,因此我单击该链接到该主题中的推文时间线,然后选择我看到的单个推文,即推文 ID1582925073509281792
。我尝试选择一个没有大量链接和主题标签的选项,以保持响应较短,否则会返回更多实体值。
然后我使用 API 获取该推文和附加注释(这是基本 HTTP 版本,而不是 Tweepy 或 Python 版本):
GET /2/tweets/1582925073509281792?tweet.fields=context_annotations,entities
结果:
{
"data": {
"entities": {
"mentions": [
{
"start": 0,
"end": 8,
"username": "tipatat",
"id": "94606087"
}
]
},
"id": "1582925073509281792",
"edit_history_tweet_ids": [
"1582925073509281792"
],
"text": "@tipatat AR is very much a part of the larger system we think of as the Metaverse…",
"context_annotations": [
{
"domain": {
"id": "46",
"name": "Business Taxonomy",
"description": "Categories within Brand Verticals that narrow down the scope of Brands"
},
"entity": {
"id": "1557697333571112960",
"name": "Technology Business",
"description": "Brands, companies, advertisers and every non-person handle with the profit intent related to softwares, apps, communication equipments, hardwares"
}
},
{
"domain": {
"id": "30",
"name": "Entities [Entity Service]",
"description": "Entity Service top level domain, every item that is in Entity Service should be in this domain"
},
"entity": {
"id": "848920371311001600",
"name": "Technology",
"description": "Technology and computing"
}
},
{
"domain": {
"id": "131",
"name": "Unified Twitter Taxonomy",
"description": "A taxonomy of user interests. "
},
"entity": {
"id": "848920371311001600",
"name": "Technology",
"description": "Technology and computing"
}
},
{
"domain": {
"id": "131",
"name": "Unified Twitter Taxonomy",
"description": "A taxonomy of user interests. "
},
"entity": {
"id": "1427745203700469767",
"name": "Metaverse"
}
},
{
"domain": {
"id": "165",
"name": "Technology",
"description": "for individual and types of technology, e.g., food technology, 3D printing"
},
"entity": {
"id": "848920371311001600",
"name": "Technology",
"description": "Technology and computing"
}
}
]
}
}
这仍然是一长串回复,但我们可以从这里开始找到我们可能感兴趣的东西。每个注释都有一个顶级domain然后是一个人id在该域内。如果我们查看列表,我们可以看到该域131
带有实体 ID1427745203700469767
好像是关于“元界”的。现在我们有一种方法可以搜索 Twitter 识别为与“Metaverse”相关的推文!
如果我们使用以下命令进行搜索context
of 131.1427745203700469767
(这是domain_id.entity_id
)然后我们可以取回一些相关的推文。我会在正文中添加augmented reality
缩小范围。我在这里再次使用了 HTTP 格式,您可以根据您选择的库修改它。
GET /2/tweets/search/recent?query=context:131.1427745203700469767 augmented reality
That (默认)在本例中是关于 Metaverse 的主题。
您将需要尝试识别可用的上下文 ID - 并非每个主题都直接由 API 中的上下文反映,并且这与您在 Twitter 应用程序本身的特定选项卡上可能看到的内容不完全匹配。
这是关于如何执行类似操作的有用帖子的链接:如何使用 Twitter API v2 搜索有关各种“主题”的推文 https://dev.to/suhemparack/how-to-search-for-tweets-about-various-topics-using-the-twitter-api-v2-3p86。我还推荐使用 TwitterAPI工具 https://developer.twitter.com/apitools and 来测试一下。