使用Tweepy提取与tweet关联的Twitter用户

时间:2019-01-24 18:11:02

标签: python

我有使用延迟循环提取一些Twitter数据的代码。我想将每个推特的关联用户添加到我的数据收集中,但是要使此推特工作非常困难...

import tweepy
from textblob import TextBlob
import csv
import time

count = 0
num = 0

numTimesToRepeat = 3

for i in range(numTimesToRepeat):
    if i>0:
        num = num + 1

        print("")
        print(num)
        print("")       

        print("NEW SEARCH BEGINS HERE " + str(num))

        consumer_key = '******'
        consumer_secret = '******'

        access_token = '******'
        access_token_secret = '******'

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

        api = tweepy.API(auth)


        public_tweets_list = api.search('"meaningful work"', count=200)


        fieldnames = ['count', 'Tweet', 'polarity_analysis', 'tweettime', 'rating']#, 'user']


        file_name = str("tweet_data" + str(num) + ".csv")

        writer = csv.DictWriter(open(file_name, "w"), fieldnames=fieldnames)
        writer.writeheader()



        for tweet in public_tweets_list:
            analysis = TextBlob(tweet.text)
            api.get_user('screen_name')#THIS LINE NOT WORKING
            print(api.get_user('screen_name'))#THIS LINE NOT WORKING


            if analysis.sentiment.polarity < -.7:
                print("")
                print(tweet.text)
                print(tweet.created_at)
                count = count + 1
                print(count)
                writer.writerow({
                    "count": str(count),
                    "Tweet": str(tweet.text),
                    "polarity_analysis": "Very Negative",
                    "tweettime": str(tweet.created_at),
                    "rating": analysis.sentiment,
                    "user": str(tweet.text) #THIS LINE NOT WORKING

                })

            elif analysis.sentiment.polarity < -.3:
                print("")
                print(tweet.text)
                print(tweet.created_at)
                count = count + 1
                print(count)
                writer.writerow({
                    "count": str(count),
                    "Tweet": str(tweet.text),
                    "polarity_analysis": "Moderately Negative",
                    "tweettime": str(tweet.created_at),
                    "rating": analysis.sentiment,
                    "user": str(tweet.text) #THIS LINE NOT WORKING

                })

            elif analysis.sentiment.polarity < 0:
                print("")
                print(tweet.text)
                print(tweet.created_at)
                count = count + 1
                print(count)
                writer.writerow({
                    "count": str(count),
                    "Tweet": str(tweet.text),
                    "polarity_analysis": "Slightlyly Negative",
                    "tweettime": str(tweet.created_at),
                    "rating": analysis.sentiment,
                    "user": str(tweet.text) #THIS LINE NOT WORKING

                })


            elif analysis.sentiment.polarity == 0:
                print("")
                print(tweet.text)
                print(tweet.created_at)
                count = count + 1
                print(count)
                writer.writerow({
                    "count": str(count),
                    "Tweet": str(tweet.text),
                    "polarity_analysis": "Neutral",
                    "tweettime": str(tweet.created_at),
                    "rating": analysis.sentiment,
                    "user": str(tweet.text) #THIS LINE NOT WORKING

                })

            elif analysis.sentiment.polarity > 0:
                print("")
                print(tweet.text)
                print(tweet.created_at)
                count = count + 1
                print(count)
                writer.writerow({
                    "count": str(count),
                    "Tweet": str(tweet.text),
                    "polarity_analysis": "Slightlyly Positive",
                    "tweettime": str(tweet.created_at),
                    "rating": analysis.sentiment,
                    "user": str(tweet.text) #THIS LINE NOT WORKING

                })

            elif analysis.sentiment.polarity > .3:
                print("")
                print(tweet.text)
                print(tweet.created_at)
                count = count + 1
                print(count)
                writer.writerow({
                    "count": str(count),
                    "Tweet": str(tweet.text),
                    "polarity_analysis": "Moderately Positive",
                    "tweettime": str(tweet.created_at),
                    "rating": analysis.sentiment,
                    "user": str(tweet.text) #THIS LINE NOT WORKING

                })

            elif analysis.sentiment.polarity > .7:
                print("")
                print(tweet.text)
                print(tweet.created_at)
                count = count + 1
                print(count)
                writer.writerow({
                    "count": str(count),
                    "Tweet": str(tweet.text),
                    "polarity_analysis": "Moderately Positive",
                    "tweettime": str(tweet.created_at),
                    "rating": analysis.sentiment,
                    "user": str(tweet.text) #THIS LINE NOT WORKING

                })

            else:
                print("")
                print(tweet.text)
                print(tweet.created_at)
                count = count + 1
                print(count)
                writer.writerow({
                    "count": str(count),
                    "Tweet": str(tweet.text),
                    "polarity_analysis": "Error",
                    "tweettime": str(tweet.created_at),
                    "rating": analysis.sentiment,
                    "user": str(tweet.text) #THIS LINE NOT WORKING

                })
        time.sleep(60) # Delay for 1 minute (60 seconds).

我得到的结果是

Traceback (most recent call last): File "SentimentAnalysis.py", line 145, in <module> api.get_user('screen_name')#THIS LINE NOT WORKING File "/anaconda3/lib/python3.7/site-packages/tweepy/binder.py", line 250, in _call return method.execute() File "/anaconda3/lib/python3.7/site-packages/tweepy/binder.py", line 234, in execute raise TweepError(error_msg, resp, api_code=api_error_code) tweepy.error.TweepError: [{'code': 63, 'message': 'User has been suspended.'}]

1 个答案:

答案 0 :(得分:0)

tweepy.error.TweepError:[{'代码':63,'消息':'用户已被暂停。'}]

Twitter确实由于某些已知原因而被暂停帐户。因此,返回的错误表明您无法继续。这是因为该帐户不存在。

选项1:试用其他用户。

选项2:继续检查直到帐户恢复(没人知道何时会发生这种情况)。