微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用Tweepy和TextBlob循环将分析导出到CSV

如何解决使用Tweepy和TextBlob循环将分析导出到CSV

我已经编写了一个应用程序,该应用程序可以对主题执行情感分析,并将推文导出到CSV文件,但是使用Tweepy和TextBlob只能导出一些推文。我想将其插入while循环中,以便它继续运行并添加到CSV文件中,直到停止程序为止。任何帮助,将不胜感激。或任何其他方式。

import tweepy
import csv
from urlextract import URLExtract
from textblob import TextBlob

consumer_key = 'KEY'
consumer_secret = 'KEY'
access_token = 'KEY'
access_token_secret = 'KEY'

auth = tweepy.OAuthHandler(consumer_key,consumer_secret)
auth.set_access_token(access_token,access_token_secret)

api = tweepy.API(auth)

public_tweets = api.search('Trump')

with open('twitter_sentiment_analysis.csv','w',encoding="utf-8",newline='') as output:
    fileOut = csv.writer(output)
    data = [['Tweets','Polarity','Subjectivity','URL']]

    fileOut.writerows(data)

    for tweet in public_tweets:
        analysis = TextBlob(tweet.text)
        polarity = analysis.sentiment.polarity
        subjectivity = analysis.sentiment.subjectivity


        url = None

        words = tweet.text.split()

        link = URLExtract()

        urls = link.find_urls(tweet.text)

        for word in words:
            # print (word)
            if 'http' in word:
                url = word

        fileOut.writerow([tweet.text,polarity,subjectivity,url])

        print(tweet.text)
        print('Polarity: ',polarity)
        print('Subjectivity:',subjectivity)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。