我正在嘗試抓取網頁(“coinmarketcap”)。我正在抓取所有加密貨幣從 2013 年到 2019 年 10 月(開盤價、最高價、最低價、收盤價、市值、成交量)的數據。for j in range (0,name_size): url = ("https://coinmarketcap.com/currencies/" + str(name[j]) + "/historical-data/?start=20130429&end=20191016") page = urllib.request.urlopen(url) soup = BeautifulSoup(page, 'html.parser') priceDiv = soup.find('div', attrs={'class':'table-responsive'})rows = priceDiv.find_all('tr')問題是某些網址不存在。我不知道如何跳過這些。你能幫我么?
2 回答

人到中年有點甜
TA貢獻1895條經驗 獲得超7個贊
利用try-except
for j in range (0,name_size):
url = ("https://coinmarketcap.com/currencies/" + str(name[j]) + "/historical-data/?start=20130429&end=20191016")
try:
page = urllib.request.urlopen(url)
soup = BeautifulSoup(page, 'html.parser')
priceDiv = soup.find('div', attrs={'class':'table-responsive'})
except:
print("Coult not open url")
rows = priceDiv.find_all('tr')

一只斗牛犬
TA貢獻1784條經驗 獲得超2個贊
使用錯誤捕獲。
try:
#do the thing
except Exception as e:
#here you can print the error
錯誤的將被打印消息跳過,否則任務繼續
添加回答
舉報
0/150
提交
取消