簡體   English   中英

該代碼給了我一個錯誤,即名稱 tweepy 未定義,並且在 tweet.text 中出現錯誤,即未定義文本屬性

[英]The code is giving me an error that the name tweepy is not defined , and there occurs error in tweet.text that text attribute is not defined

from textblob import TextBlob import sys import tweepy import matplotlib.pyplot as plt import pandas as pd import numpy as np import os import nltk import pycountry import re import string #情緒分析 def percent(part,whole): return 100 * float(part) /float(whole) keyword = input("請輸入要搜索的關鍵字或標簽:") noOfTweet = int(input ("請輸入要分析的推文數量:")) tweets = tweepy.Cursor(api.search_users, q=關鍵字).items(noOfTweet) 正面 = 0 負面 = 0 中性 = 0 極性 = 0 推文列表 = [] 中性列表 = [] 負面列表 = [] 正面列表 = [] 推文中的推文:

 #print(tweet.text)
 tweet_list.append(tweet.data)
 analysis = TextBlob(tweet.data)
 score = SentimentIntensityAnalyzer().polarity_scores(tweet.text)
 neg = score['neg']
 neu = score['neu']
 pos = score['pos']
 comp = score['compound']
 polarity += analysis.sentiment.polarity
 negative += 1
 if(pos > neg):
   positive_list.append(tweet.text)
   positive += 1
 
 elif pos == neg:
  neutral_list.append(tweet.text)
  neutral += 1
positive = percentage(positive, noOfTweet)
negative = percentage(negative, noOfTweet)
neutral = percentage(neutral, noOfTweet)
polarity = percentage(polarity, noOfTweet)
positive = format(positive, '.1f')
negative = format(negative, '.1f')
neutral = format(neutral, '.1f')

您正在使用api.search_users()方法,該方法返回User對象而不是Tweet對象(請參閱文檔),因此它們沒有任何text屬性。

您應該改用api.search_tweets()方法(請參閱文檔)。

暫無
暫無

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

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