从 Twitter 导入数据进行情绪分析时,如何消除意外的参数和属性错误?
How to remove unexpected parameter and attribute errors while importing data for sentiment analysis from twitter?
问)
如何解决以下错误
1) 意外参数:Lang
2) 意外参数:tweet_node
3) 第 25 行,在
tweets = [tweet.full_text 用于 tweet_cursor ]
中的推文
AttributeError: 'Status' 对象没有属性 'full_text'
代码
import tweepy
import textblob
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import re
api_key = 'xxxxx'
api_key_secret = 'xxxxxxx'
access_token = 'xxxxxxxxxxxxxxxx'
access_token_secret = 'xxxxxxxxxxxxxxxxxxxxxx'
authenticator = tweepy.OAuthHandler(api_key, api_key_secret)
authenticator.set_access_token(access_token, access_token_secret)
api= tweepy.API (authenticator, wait_on_rate_limit=True)
crypto_currency= "Dogecoin"
search= f'#(crypto currency) -filter: retweets'
tweet_cursor = tweepy.Cursor(api.search_tweets, q=search, Lang='en', `tweet_node='extended').items (100)`
tweets = [tweet.full_text for tweet in tweet_cursor ]
tweets_df = pd.DataFrame(tweets , columns = ['Tweets'])
for _, row in tweets_df.iterrows():
row ['tweets'] = re.sub('https\S+','' , row['Tweets'])
row['tweets'] = re.sub('#\S+', '', row['Tweets'])
row['tweets'] = re.sub('@\S+', '', row['Tweets'])
row['tweets'] = re.sub('\n', '', row['Tweets'])
tweets_df['Polarity'] = tweets_df['Tweets'].map(lambda tweet:textblob.TextBlob(tweet).sentiment.polarity)
tweets_df['Result'] = tweets_df['Polarity'].map(lambda pol:'+' if pol > 0 else '-')
positive = tweets_df[tweets_df.Result == '+'].count()['Tweets']
negative = tweets_df[tweets_df.Result == '-'].count()['Tweets']
plt.bar([0,1], [positive,negative], label=['Positive', 'Negative'], color=['green', 'red'])
plt.legend
plt.show()
- lang=en 应该在
search
. 的值内
- tweet_node 应该是
tweet_mode
- 只有
tweet_mode=extended
参数正确,并且推文的文本长度超过 140 个字符,full_text
才会存在。
问) 如何解决以下错误
1) 意外参数:Lang
2) 意外参数:tweet_node
3) 第 25 行,在 tweets = [tweet.full_text 用于 tweet_cursor ]
中的推文AttributeError: 'Status' 对象没有属性 'full_text'
代码
import tweepy
import textblob
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import re
api_key = 'xxxxx'
api_key_secret = 'xxxxxxx'
access_token = 'xxxxxxxxxxxxxxxx'
access_token_secret = 'xxxxxxxxxxxxxxxxxxxxxx'
authenticator = tweepy.OAuthHandler(api_key, api_key_secret)
authenticator.set_access_token(access_token, access_token_secret)
api= tweepy.API (authenticator, wait_on_rate_limit=True)
crypto_currency= "Dogecoin"
search= f'#(crypto currency) -filter: retweets'
tweet_cursor = tweepy.Cursor(api.search_tweets, q=search, Lang='en', `tweet_node='extended').items (100)`
tweets = [tweet.full_text for tweet in tweet_cursor ]
tweets_df = pd.DataFrame(tweets , columns = ['Tweets'])
for _, row in tweets_df.iterrows():
row ['tweets'] = re.sub('https\S+','' , row['Tweets'])
row['tweets'] = re.sub('#\S+', '', row['Tweets'])
row['tweets'] = re.sub('@\S+', '', row['Tweets'])
row['tweets'] = re.sub('\n', '', row['Tweets'])
tweets_df['Polarity'] = tweets_df['Tweets'].map(lambda tweet:textblob.TextBlob(tweet).sentiment.polarity)
tweets_df['Result'] = tweets_df['Polarity'].map(lambda pol:'+' if pol > 0 else '-')
positive = tweets_df[tweets_df.Result == '+'].count()['Tweets']
negative = tweets_df[tweets_df.Result == '-'].count()['Tweets']
plt.bar([0,1], [positive,negative], label=['Positive', 'Negative'], color=['green', 'red'])
plt.legend
plt.show()
- lang=en 应该在
search
. 的值内
- tweet_node 应该是
tweet_mode
- 只有
tweet_mode=extended
参数正确,并且推文的文本长度超过 140 个字符,full_text
才会存在。