使用tweepy获取tweet ID的元数据已不起作用



我使用tweetpy库(用于twitter api-v1.1(为tweet ID列表获取一些元数据(例如,tweet文本、#retweets、userid等(。这是我的代码:

consumer_key = 'xxxxxxxxxxxx'
consumer_key_secret = 'xxxxxxxxxxxx'
access_token = 'xxxxxxxxxxxxxxxxxx'
access_token_secret = 'xxxxxxxxxxxxxxxxxx'
auth = tweepy.OAuthHandler(consumer_key, consumer_key_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
def createTrainingSet(corpusFile, tweetContent):
import csv
import time
import json
counter = 0
corpus = []
with open(corpusFile, 'r') as csvfile:
lineReader = csv.reader(csvfile, delimiter=',')
for row in lineReader:
corpus.append({"tweet_id": row[0], "unreliable": row[1], "conspiracy": row[2],
"clickbait": row[3], "political/biased": row[4], "date": row[5]})
sleepTime = 2
trainingDataSet = []  

for tweet in corpus:
try:
tweetFetched = api.get_status(tweet["tweet_id"])
print("Tweet fetched" + tweetFetched.text)
print("followers_count: "+ str(tweetFetched.user.followers_count))
print("friends_count: " + str(tweetFetched.user.friends_count))

tweet["text"] = tweetFetched.text
tweet["retweet_count"] = tweetFetched.retweet_count
tweet["favorite_count"] = tweetFetched.favorite_count
tweet["created_at"] = tweetFetched.created_at   
tweet["user_id"] = tweetFetched.user.id_str
tweet["user_created_at"] = tweetFetched.user.created_at              

trainingDataSet.append(tweet)
time.sleep(sleepTime)
except:
print("Inside the exception - no:2")
continue
# This is corpus dataset
corpusFile = "sample.csv"
# This is my target file
tweetContent = "tweetContent.csv"
# Call the method
resultFile = createTrainingSet(corpusFile, tweetContent)

我不知道为什么这个代码不能再工作了(上一次工作是在几个月前(。但是,当我现在运行它时,它会返回"Inside the exception - no:2"。为什么?

以下是帮助我找到错误的两行代码:

except tweepy.TweepError as e:
print ('the error code:', e.args[0][0]['code'])
print ('the error message:', e.args[0][0]['message'])  

此外,感谢Jeyekomon在这篇文章中的回答,我发现e.message[0]['code']不再工作:

错误代码过去是使用e.message[0]["代码"]访问的,现在已不起作用。message属性在Python 2.6中已被弃用,在Python 3.0中已被删除。当前您收到一个错误"TweepError"对象没有属性"message">

此外,TweepError异常类中似乎还有一些文档中没有的其他有用属性(api_codereasonresponse(。

最新更新