使用 tweepy 获取推文 ID 的元数据不再有效

问题描述

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

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's answer in this post,我发现 e.message[0]['code'] 不再工作了:

过去使用 e.message[0]['code'] 访问的错误代码不再有效。 message 属性已在 Python 2.6 中弃用,并在 Python 3.0 中删除。当前您收到错误 'TweepError' 对象没有属性 'message'

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