Python Tweepy 语法错误
Python Syntax Error with Tweepy
祝各位码农新年快乐!
我之前在 heroku 上编译 Python 时遇到了问题,它抛出了缩进错误并得到了这个线程的回答。
无论如何,在你们的帮助下,我通过了上述错误,但是弹出了一个奇怪的语法错误。
banned_accs = ['@bbc' ,'@cnn']
for tweet in tweepy.Cursor(api.search, q='#test').items():
if not any(acc == tweet.user.screen_name for acc in banned_accs):
tweet.retweet()
print('Retweeted the tweet')
# Favorite the tweet
tweet.favorite()
print('Favorited the tweet')
# Follow the user who tweeted
tweet.user.follow()
print('Followed the user')
sleep(5)
else:
pass
except tweepy.TweepError as e:
print(e.reason)
except StopIteration:
break
从控制台生成的错误是
except tweepy.TweepError as error:
^
SyntaxError: invalid syntax
有人可以帮我解决这个问题吗?提前致谢。
您有 except
个方块,但我没有看到 try
个方块。也许您没有将整个代码包含在问题中。
很可能您在代码上方某处使用了 try
关键字,或者您忘记使用 try
块。 except
应该与 try
的缩进相同,如果有 try
块或者如果您忘记了 try
块然后在可能抛出异常的代码块上方声明它.
并且 print(e.reason)
应该在 except
块内缩进
祝各位码农新年快乐!
我之前在 heroku 上编译 Python 时遇到了问题,它抛出了缩进错误并得到了这个线程的回答。
banned_accs = ['@bbc' ,'@cnn']
for tweet in tweepy.Cursor(api.search, q='#test').items():
if not any(acc == tweet.user.screen_name for acc in banned_accs):
tweet.retweet()
print('Retweeted the tweet')
# Favorite the tweet
tweet.favorite()
print('Favorited the tweet')
# Follow the user who tweeted
tweet.user.follow()
print('Followed the user')
sleep(5)
else:
pass
except tweepy.TweepError as e:
print(e.reason)
except StopIteration:
break
从控制台生成的错误是
except tweepy.TweepError as error:
^
SyntaxError: invalid syntax
有人可以帮我解决这个问题吗?提前致谢。
您有 except
个方块,但我没有看到 try
个方块。也许您没有将整个代码包含在问题中。
很可能您在代码上方某处使用了 try
关键字,或者您忘记使用 try
块。 except
应该与 try
的缩进相同,如果有 try
块或者如果您忘记了 try
块然后在可能抛出异常的代码块上方声明它.
并且 print(e.reason)
应该在 except
块内缩进