for 循环中出现不可预测异常时,跳过本次循环并 print 出错误内容,继续执行下一次循环应该如何写?
现在使用的是 try...except...这样的,使用 continue 方法.
但是在实测中,如果连接失败,程序之间停止.
在代码中可以看出,是使用的 bs 抓取 url.程序循环到其中某一条连接时,内容里没有包含指定 a 标签的内容的话,也会报错并终止程序.
应该如何实现继续循环?
for u in product_page_url:
a = requests.get(u.strip())
soup = BeautifulSoup(a.text, 'html.parser')
try:
clean_downurl = soup.find(class_ = 'downurl').a['href']
b.append(clean_downurl)
print u
except:
continue
现在使用的是 try...except...这样的,使用 continue 方法.
但是在实测中,如果连接失败,程序之间停止.
在代码中可以看出,是使用的 bs 抓取 url.程序循环到其中某一条连接时,内容里没有包含指定 a 标签的内容的话,也会报错并终止程序.
应该如何实现继续循环?
for u in product_page_url:
a = requests.get(u.strip())
soup = BeautifulSoup(a.text, 'html.parser')
try:
clean_downurl = soup.find(class_ = 'downurl').a['href']
b.append(clean_downurl)
print u
except:
continue