2
我使用下面的代碼來獲取網站的標題。使用BeautifulSoup解析網頁 - 跳過404錯誤頁面
from bs4 import BeautifulSoup
import urllib2
line_in_list = ['www.dailynews.lk','www.elpais.com','www.dailynews.co.zw']
for websites in line_in_list:
url = "http://" + websites
page = urllib2.urlopen(url)
soup = BeautifulSoup(page.read())
site_title = soup.find_all("title")
print site_title
如果網站的列表中包含一個「壞」(不存在)的網站/網頁或網站有某種或錯誤,例如「404頁找不到」等,該腳本將打破和停止。
以什麼方式我可以有腳本忽略/跳過「壞」(不存在)和有問題的網站/網頁?
感謝帕德里克·坎寧安,它的速度與激情! –
不用擔心,不客氣。 –