如何使用 tweepy 获取推文的全文? [英] How to get the full text of a tweet using tweepy?
问题描述
我正在使用 tweepy.Cursor 来提取特定主题的过去推文,但是如果推文真的很长,它会截断它.我使用 full_text 属性为 True,但它仍然没有修复它.如何解决这个问题?
I'm using tweepy.Cursor to extract past tweets of a particular topic, however if the tweet is really long it truncates it. I am using the full_text property to be True, but still it doesn't fix it. How to fix this?
我的代码在这里:
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()
推荐答案
您必须显式访问名为full_text"的字段.你可以试试这样的:
you have to explicitly access the field called "full_text". You can try something like this:
# 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 文件或任何您想要的内容.
You can extract as much info as you need and then write it into a CSV file or whatever you want.
我建议您将推文提取到一个 json 文件中,以便您可以轻松检查它提供给您的所有字段.
I recomend you extract the tweets into a json file so you can easily check all the fields it offers to you.
希望有帮助!
编辑:如果检索到的推文是RT,全文将在result["retweeted_status"]["full_text"]
Edit: If the retrieved tweet is a RT, the full text will be in result["retweeted_status"]["full_text"]
这篇关于如何使用 tweepy 获取推文的全文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!