尽管使用了扩展文本功能,Tweepy仍然没有返回全文

2024-09-28 01:34:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用tweepy下载关于某个特定主题的tweet,但是没有人知道我遵循哪个教程,我无法将tweet输出为完整的tweet。总是有一个椭圆在一定数量的字符后将其截断

这是我正在使用的代码

import json
import tweepy
from tweepy import OAuthHandler 
import csv
import sys
from twython import Twython
nonBmpMap = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd)
with open ('Twitter_Credentials.json') as cred_data:
info = json.load(cred_data)
consumer_Key = info['Consumer_Key']
consumer_Secret = info['Consumer_Secret']
access_Key = info['Access_Key']
access_Secret = info['Access_Secret']


maxTweets = int(input('Enter the Number of tweets that you want to extract '))
userTopic = input('What topic do you want to search for ')
topic = ('"' + userTopic + '"')
tweetCount = 0

auth = OAuthHandler(consumer_Key, consumer_Secret)
auth.set_access_token(access_Key, access_Secret)
api = tweepy.API(auth, wait_on_rate_limit=True)
tweets = api.search(q=topic, count=maxTweets, tweet_mode= 'extended')

for tweet in tweets:
    tweetCount = (tweetCount+1)
    with open ('TweetsAbout' + userTopic, 'a', encoding='utf-8') as the_File:
        print(tweet.full_text.translate(nonBmpMap))
        tweet = (str(tweet.full_text).translate(nonBmpMap).replace(',','').replace('|','').replace('\n','').replace('’','\'').replace('…',"end"))
        the_File.write(tweet + "\n")
        print('Extracted ' + str(tweetCount) + ' tweets about ' + topic)

Tags: thekeyimportinfojsonsecrettopicaccess
1条回答
网友
1楼 · 发布于 2024-09-28 01:34:02

试试这个,看看能不能用

try:
    specific_tweets = tweepy.Cursor(api.search, tweet_mode='extended', q=<your_query_string> +" -filter:retweets", lang='en').items(500)
except tweepy.error.TweepError:
    pass
for tweet in specific_tweets:
    extracted_text = tweet.full_text

您试图提取的所有文本都应该在extracted_text中。祝你好运

相关问题 更多 >

    热门问题