如何使用 tweepy 获取推文的全文?
How to get the full text of a tweet using 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()
您必须明确访问名为 "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"]
我正在使用 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()
您必须明确访问名为 "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"]