我有一个从推特收集的数据集,其中包括推文以及推文发布者帐户的用户名和屏幕名
对于数据集中的每个用户,我想检索关注者列表和关注帐户列表。代码正在运行,大约半小时后我收到了这个错误
未授权:401未授权 未授权。
我使用另一个代码测试了 API 授权,它正在工作
import tweepy as tw
import pandas as pd
# Read Consumer and Access Token keys from "auth.txt"
filepath = 'auth.txt'
with open(filepath, "r") as file:
keys = file.readlines()
auth = tw.auth.OAuthHandler(keys[0].replace("\n",""), keys[1].replace("\n",""))
auth.set_access_token(keys[2].replace("\n",""), keys[3].replace("\n",""))
tw_api = tw.API(auth, wait_on_rate_limit=True)```
tweets = pd.read_csv("/content/drive/MyDrive/Phd_DS/Final/All_DS_Features_complete_multi2.csv")
# Convert our tweet_url Series into a list
ids = tweets['screen_name'].tolist()
# Process the ids by batch or chunks.
total_count = len(ids)
chunks = (total_count - 1) // 50 + 1
def fetch_tw(ids):
list_of_tw_status = tw_api.lookup_users(screen_name=ids)
empty_data = pd.DataFrame()
for user in list_of_tw_status:
tweet_elem = {
"favorite_list": tw_api.get_favorites(screen_name = user.screen_name),
"followers_list": tw_api.get_followers(screen_name = user.screen_name),
"friends_list": tw_api.get_friends(screen_name = user.screen_name)
}
empty_data = empty_data.append(tweet_elem, ignore_index = True)
df = tweets.join(empty_data)
df.to_csv("test.csv", mode="a")
# Create another for loop to loop into our batches while processing 50 entries every loop
for i in range(chunks):
batch = ids[i*50:(i+1)*50]
result = fetch_tw(batch)