我的申请是使用Tweepy转发标签。转发标签工作,我无法让错误2和3工作。 错误 1.你自己的id(完成) 2.如果推文已经被RTd 3.如果RT的推文来自受保护的来源
在StdOutListener中访问api.retweet(doTweet)不允许它落入on_error()。我怎么能这样做?我是Python noob。
class StdOutListener(tweepy.StreamListener):
def on_data(self, data):
all_data = json.loads(data)
username = all_data["user"]["screen_name"]
doTweet = all_data["id"]
if username != our_own_id:
#make sure you haven't already retweeted
#make sure tweets aren't protected
print(username) # just so we know it's working
api.retweet(doTweet)
return True
def on_error(self, status_code):
print('error')
read_error = json.loads(status_code)
print('Got an error with status code: ' + str(read_error))
return True # To continue listening
def on_timeout(self):
print('Timeout...')
return True # To continue listening
try:
if __name__ == '__main__':
listener = StdOutListener()
stream = tweepy.Stream(auth, listener)
stream.filter(track=['#love'])
except KeyboardInterrupt:
sys.exit()
答案 0 :(得分:2)
您可以在api.retweet
方法中修改对on_data
的通话,如下所示:
...
if username != our_own_id:
print(username) # just so we know it's working
try:
api.retweet(doTweet)
except tweepy.TweepError as e:
# add here a more complex error handling
print(e)
希望它有所帮助。