Python网页抓取:如何跳过url错误

我正在尝试抓取网页(“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')

问题是某些网址不存在。我不知道如何跳过这些。你能帮我么?


料青山看我应如是
浏览 348回答 2
2回答

人到中年有点甜

利用try-exceptfor 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')

一只斗牛犬

使用错误捕获。try:     #do the thingexcept Exception as e:    #here you can print the error错误的将被打印消息跳过,否则任务继续
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python