首页 > 解决方案 > 如何使用 tweepy 获取推文的全文?

问题描述

我正在使用 tweepy.Cursor 提取特定主题的过去推文,但是如果推文真的很长,它会截断它。我正在使用 full_text 属性为 True,但它仍然无法修复它。如何解决这个问题?

我的代码在这里:

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

API = tweepy.API(auth)

csvFile = open('tweets2.csv', 'a')
csvWriter = csv.writer(csvFile)


for tweet in tweepy.Cursor(API.search,q="$EURUSD",count=1000,
                       lang="en", full_text = True).items():

csvWriter.writerow([tweet.created_at, tweet.text.encode('utf-8')])

csvFile.close()

标签: tweepy

解决方案


您必须显式访问名为“full_text”的字段。你可以尝试这样的事情:

# First you get the tweets in a json object
results = [status._json for status in tweepy.Cursor(API.search, q="$EURUSD", count=1000, tweet_mode='extended', lang='en').items()]

# Now you can iterate over 'results' and store the complete message from each tweet.
    my_tweets = []
    for result in results:
        my_tweets.append(result["full_text"])

您可以根据需要提取尽可能多的信息,然后将其写入 CSV 文件或任何您想要的文件。

我建议您将推文提取到 json 文件中,以便您可以轻松检查它提供给您的所有字段。

希望能帮助到你!

编辑:如果检索到的推文是 RT,则全文将在 result["retweeted_status"]["full_text"]


推荐阅读