Tweepy出现Python语法错误
祝各位程序员新年快乐! 我之前在heroku上编译Python时遇到了问题,它抛出了缩进错误,并得到了这个线程的回答。 无论如何,在你们的帮助下,我传递了上面提到的错误,但是一个奇怪的语法错误突然出现Tweepy出现Python语法错误,python,heroku,twitter,tweepy,Python,Heroku,Twitter,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
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
有人能帮我弄清楚吗?提前感谢。除了块之外,您还有块,但我没有看到try块。也许您没有在问题中包含您的全部代码 最有可能的情况是,您正在代码上方的某个位置使用
try
关键字,或者您忘记使用try
块<如果存在try
块,或者如果忘记了try
块,则在可能引发异常的代码块上方声明它
而
print(e.reason)
应该缩进块中,除非块中似乎没有try
。这需要在您预期可能引发异常的代码部分之上,并且所有内容都应该在该块中缩进。这在Python中是标准的,与tweepy无关。你的try块从哪里开始?我犯的胡说八道的错误。对不起,我不得不把这个放在这里。谢谢你们的回复。投票删除此问题。Ofc。错过一件多么简单的事啊。谢谢,代码没有尝试捕捉异常。我自己犯了个大错:/