首页 > 解决方案 > 使用推特进行情绪分析

问题描述

我在从 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)

标签: python-3.xtwittersentiment-analysis

解决方案


推荐阅读