簡體   English   中英

Tweepy - 'tweet.text' 不檢索文本

[英]Tweepy - 'tweet.text' not retrieving text

我是 Python、API 和一般編碼的新手。

我正在研究 Twitter 情緒分析機器人,但我無法弄清楚為什么它不返回text字段。 每次我運行腳本時,PyCharm 都會給我AttributeError: 'list' object has no attribute 'text' 有人可以幫忙嗎?

我在這些行上方定義了我的令牌和 API 密鑰:

def authentication(cons_key, cons_secret, access_token, access_secret):
    auth = tw.OAuthHandler(cons_key, cons_secret)
    auth.set_access_token(acces_token, access_secret)
    api = tw.API(auth, wait_on_rate_limit=True)

#Sentiment Analysis Block
query = input('Enter keyword/hashtag to search: ')
#numtweets = int(input('How many tweets you would like analyzed: ')) #update with paginator

#Defining percentage, for use on percent of sentiment
#def percentage(part, whole):
#    return 100 *float(part)/float(whole)

tweets = client.search_recent_tweets(query=query, max_results=100)
#positive = 0
#neutral = 0
#negative = 0
#polarity = 0
#tweet_list = []
#positive_list = []
#neutral_list = []
#negative_list = []

for tweet in tweets:
    print(tweet.text)
    #tweet_list.append(str(tweet.text))
    #analysis = TextBlob(tweet.text)
    #score = SentimentIntensityAnalyzer().polarity_scores(tweet.text)
    #pos = score['pos']
    #neu = score['neu']
    #neg = score['neg']
    #comp = score['compound']
    #polarity += analysis.sentiment.polarity

一旦腳本到達print(tweet.text) ,它就會給我錯誤。 任何反饋?

tweets是一個tweepy.Response (名為元組)object。您迭代的是它的字段而不是 Tweet 對象,它的第一個data字段是 Tweet 對象的列表。 因此,列表沒有text屬性的錯誤。 您可能想要遍歷data字段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM