gpt4 book ai didi

python - 使用请求模块通过 Python 在 json 中打印 Twitter 句柄的方法?

转载 作者:行者123 更新时间:2023-12-04 07:59:53 25 4
gpt4 key购买 nike

我正在制作一个 Twitter 机器人来在 Twitter 上搜索最近推文中的特定关键字和短语。我一直在使用this文档作为指南,它使用 Python requests 模块。

import requests
import json

BEARER_TOKEN = "XYZ"

#define search twitter function
def search_twitter(query, tweet_fields, bearer_token = BEARER_TOKEN):
headers = {"Authorization": "Bearer {}".format(bearer_token)}

url = "https://api.twitter.com/2/tweets/search/recent?query={}&{}".format(
query, tweet_fields
)
response = requests.request("GET", url, headers=headers)

print(response.status_code)

if response.status_code != 200:
raise Exception(response.status_code, response.text)
return response.json()

#search term
query = "new music"
#twitter fields to be returned by api call
tweet_fields = "tweet.fields=text,author_id,created_at"

#twitter api call
json_response = search_twitter(query=query, tweet_fields=tweet_fields, bearer_token=BEARER_TOKEN)
#pretty printing
print(json.dumps(json_response, indent=4, sort_keys=True))
当我在终端中运行它时一切正常,但我似乎无法找到一种方法来打印每个 Tweet 的关联 Twitter 句柄。我找不到具体的文档/语法。
我知道我必须编辑这行代码以包含 Twitter 句柄:
tweet_fields = "tweet.fields=text,author_id,created_at"
简单地说,我还想打印与这些推文相关的实际 Twitter 句柄。任何和所有信息将不胜感激。

包括扩展的新代码:
import requests
import json
#its bad practice to place your bearer token directly into the script (this is just done for illustration purposes)
BEARER_TOKEN = "XYZ"
#define search twitter function
def search_twitter(query, tweet_fields, expansions, bearer_token = BEARER_TOKEN):
headers = {"Authorization": "Bearer {}".format(bearer_token)}

url = "https://api.twitter.com/2/tweets/search/recent?query={}&{}".format(
query, tweet_fields, expansions
)
response = requests.request("GET", url, headers=headers)

print(response.status_code)

if response.status_code != 200:
raise Exception(response.status_code, response.text)
return response.json()

#search term
query = "new music"
#twitter fields to be returned by api call
# twitter fields to be returned by api call
tweet_fields = "tweet.fields=author_id,created_at"
expansions = "expansions=author_id"

# twitter api call
json_response = search_twitter(query=query, tweet_fields=tweet_fields, expansions=expansions, bearer_token=BEARER_TOKEN)
#pretty printing
print(json.dumps(json_response, indent=4, sort_keys=True))

最佳答案

您可以使用expansions在 v2 Twitter API 中,在同一个调用中取回关联的用户对象。
说明这一点的最简单方法是使用一条推文。例如,使用 curl :

$ curl 'https://api.twitter.com/2/tweets/1212092628029698048?tweet.fields=author_id,created_at&expansions=author_id' --header 'Authorization: Bearer $BEARER_TOKEN'
这会请求带有默认字段( textid 的推文,因此您不必特别请求它们)以及 author_idcreated_at值,然后询问 author_id aka User 对象也将在响应中扩展:
{
"data": {
"id": "1212092628029698048",
"author_id": "2244994945",
"created_at": "2019-12-31T19:26:16.000Z",
"text": "We believe the best future version of our API will come from building it with YOU. Here’s to another great year with everyone who builds on the Twitter platform. We can’t wait to continue working with you in the new year. <short url removed for SO posting>"
},
"includes": {
"users": [
{
"id": "2244994945",
"name": "Twitter Dev",
"username": "TwitterDev"
}
]
}
您将在 includes.users.username 中找到 Twitter 用户的句柄。 field 。
因此,在您的代码中,您可以这样做:
# twitter fields to be returned by api call
tweet_fields = "tweet.fields=author_id,created_at"
expansions = "expansions=author_id"

# twitter api call
json_response = search_twitter(query=query, tweet_fields=tweet_fields, expansions=expansions, bearer_token=BEARER_TOKEN)
(也将 expansions 添加到 search_twitter 函数中作为输入,并用于 url 字符串格式)
对于您可能从搜索调用返回的推文数组/列表,请注意您可能需要查找 includes , 因为你会得到 data (推文对象)然后 includes (用户对象 + 您可能请求的任何其他扩展),如果同一用户在推文列表中出现多次,则它只会在 includes 中返回一次- 在这种情况下,匹配 author_id找到 username 的值对于相关的推文。

关于python - 使用请求模块通过 Python 在 json 中打印 Twitter 句柄的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66529553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com