我有一个for循环浏览搜索列表。我想检查一下搜索是否会抛出错误。我想通过确定是否存在特定类来实现此目的。如果错误类确实存在,我希望for循环移动到下一个项目。这是代码:
for each in open('temp.txt'):
soup = BeautifulSoup(ip)
alert = soup.find("div", class_="alert alert-danger")
if alert is not None:
i = soup.findAll("span", class_="name")
for x in i:
*do something*
else:
*move to the next "each" in the for loop within temp.txt*
我尝试过“继续”或“下一步”但没有成功。如果我能得到一些指导,我真的很感激。谢谢!
答案 0 :(得分:2)
您根本不需要else
部分,如果alert is None
,循环将继续到下一个项目:
for each in open('temp.txt'):
soup = BeautifulSoup(ip)
alert = soup.find("div", class_="alert alert-danger")
if alert is not None:
i = soup.findAll("span", class_="name")
for x in i:
*do something*