Python Pandas错误和异常处理



是否可以使用pandas模块pd.read_html处理执行/错误?如果HTML是空的或者没有表怎么办?我该怎么办?感谢

最简单的try/except块应该完成这项工作:

import pandas
url = 'http://example.com'
try:     
page = pandas.read_html(url, attrs={}, header = 0)
except Exception as e:
print(type(e), e)

知道你的代码引发了什么异常,你可以添加更具体的处理,比如:

import pandas
url = 'http://example.com'
try:     
page = pandas.read_html(url, attrs={}, header = 0)
except YourSpecificException as e:
# handle YourSpecificException
except Exception as e:
# handle all other exceptions

最新更新