我已经获得了使用延迟循环提取一些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.'}]
发布于 2019-01-24 18:15:41
tweepy.error.TweepError:{‘代码’:63,‘消息’:‘用户已挂起。’}
Twitter确实因为他们所知道的某些原因而被暂停账号。因此,返回的错误表明您无法继续。这是因为该帐户不存在。
选项1:尝试另一个用户。
选项2:继续检查,直到帐户恢复(没有人知道什么时候会发生)。
https://stackoverflow.com/questions/54352962
复制相似问题