python-3.x - 使用推特进行情绪分析
问题描述
我在从 twitter 获取 10 家公司的数据时遇到了一些问题,我正在为每家公司从 twitter 获取 10000 条记录并对其执行情绪分析,但是在获取记录时它卡住了,然后它停止了!我正在获取执行的数据情绪分析并创建单独的文件,然后合并到一个文件中,但代码在为第一家公司本身获取数据时卡住了
import tweepy
from textblob import TextBlob
import pandas as pd
Access_token="`access token here`"
Access_token_secret="`access token secret here`"
Consumer_key="`consumer key here`"
Consumer_secret_key="`consumer secret key here`"
auth=tweepy.OAuthHandler(Consumer_key,Consumer_secret_key)
auth.set_access_token(Access_token,Access_token_secret)
api=tweepy.API(auth)
search_list = ['company names here in a list']
for n in search_list:
positive_tweet = []
negative_tweet = []
neutral_tweet = []
Final_list = []
new_list = []
tweets = tweepy.Cursor(api.search, q=str(n) + '-filter:retweets', lang='en', since='2018-01-01').items(10000)
for tweet in tweets:
print(tweet.text)
analysis = TextBlob(tweet.text)
if (analysis.sentiment.polarity) > 0:
positive_tweet.append(analysis.sentiment.polarity)
elif analysis.sentiment.polarity < 0:
negative_tweet.append(analysis.sentiment.polarity)
elif analysis.sentiment.polarity == 0:
neutral_tweet.append(analysis.sentiment.polarity)
else:
print('Emotionless & No Opinion regarding tweet')
total = len(positive_tweet) + len(negative_tweet) + len(neutral_tweet)
percentage_of_postive_review = float(len(positive_tweet) / (total) * 100)
new_list.append(n)
new_list.append(percentage_of_postive_review)
# print(new_list)
if new_list != []:
label = ['Company Name', 'Review']
i = 0
j = 2
while i < j and j <= len(new_list):
Final_list.append(new_list[i:j])
i = i + 2
j = j + 2
df = pd.DataFrame.from_records(Final_list, columns=label)
# print(df)
df.to_csv('D:/Review_Rating/Company Review' + str(n) + '.csv', index=False)
解决方案
推荐阅读
- java - SQL从两个表中获取某个值并使结果集多行
- python - 向通过 py2neo 提示连接的图形添加了一个节点,但 MATCH (n) RETURN (n) 在 Neo4j 浏览器中显示空图形
- javascript - node-fetch 不适用于 AWS lambda 函数(“找不到 node-fetch”)
- oracle - 我可以使用一个表中的数据创建更新触发器来更新另一个表吗?
- r - R在data.frame的所有行上应用距离函数
- reactjs - 使用 Firebase 身份验证时删除登录组件的 flash (React)
- python - 如何在 Python 中分割图像并保存分割的新图像
- linux - 如何使用 unix 命令从 csv 文件中拆分特定列的值
- python-3.x - Zeep SOAP 复杂标头
- java - “Hello World”应用程序中的错误“消息:尾随部分不允许内容”