如何在python中使用search_all_tweets查找将用户名和姓名列添加到pandas dataframe中.



我正在尝试使用Twitter API收集2022年的推文。我可以记录推文的tweet_fields,但我不知道如何为每条推文的用户名和名称(user_fields)添加列。

我运行以下代码:

import requests
import os
import json
import tweepy
import pandas as pd
from datetime import timedelta
import datetime
bearer_token = "my_bearer_token_here"
keyword = "#WomeninSTEM"
start_time = "2022-01-01T12:01:00Z"
end_time = "2023-01-01T12:01:00Z"
client = tweepy.Client(bearer_token=bearer_token)
responses = client.search_all_tweets(query = "#WomeninSTEM", max_results= 500, start_time=start_time, end_time = end_time, 
user_fields = ["username", "name"],
tweet_fields =["in_reply_to_user_id", "author_id", "lang",
"public_metrics", "created_at", "conversation_id"])
**##I can't get the username or name columns to work here.** 
column = []
for i in range(len(responses.data)) :
row = []
Username = responses.data[i]["username"]
row.append(Username)
name = responses.data[i]["name"]
row.append(name)
text = responses.data[i].text
row.append(text)
favoriteCount = responses.data[i].public_metrics["like_count"]
row.append(favoriteCount)
retweet_count = responses.data[i].public_metrics["retweet_count"]
row.append(retweet_count)
reply_count = responses.data[i].public_metrics["reply_count"]
row.append(reply_count)
quote_count = responses.data[i].public_metrics["quote_count"]
row.append(quote_count)
created = responses.data[i].created_at
row.append(created)
ReplyTo = responses.data[i].text.split(" ")[0]
row.append(ReplyTo)
ReplyToUID = responses.data[i].in_reply_to_user_id
row.append(ReplyToUID)
ConversationID = responses.data[i]["conversation_id"]
row.append(ConversationID)
column.append(row)
data = pd.DataFrame(column)

每当我尝试包含用户名和名称时,我得到这个错误:KeyError Traceback (most recent call last)

假设您正在查询https://api.twitter.com/2/tweets/[…],响应没有'username'或'name'参数,这就是为什么您在试图访问它们时得到KeyError。

它确实有一个'author_id'参数,您可以使用它在https://api.twitter.com/2/users/:id上执行额外的查询并检索'username'和'name'。

更多信息在这里和这里。

相关内容

  • 没有找到相关文章

最新更新