首页 > 解决方案 > Python web scraping : how to skip url error

问题描述

I am trying to scrape a webpage ("coinmarketcap"). I am scraping data from 2013 to 2019 October (Open, High, Low, Close, Marketcap, Volume) of all cryptocurrencies.

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')

The problem is some url doesn't exist. And I don't know how to skip those. Can you please help me?

标签: pythonbeautifulsoupweb-crawlerscreen-scraping

解决方案


使用错误捕获。

try: 
    #do the thing
except Exception as e:
    #here you can print the error

错误的将被打印消息跳过,否则任务继续


推荐阅读