如果找不到任何表,则pd.read_html"无"



如果表存在,我想打印出来。

import pandas as pd
main_url='https://fbref.com/en/comps/9/2000-2001/2000-2001-Premier-League-Stats'
squad_advanced_goalkeeping=pd.read_html(main_url,match='Squad Advanced Goalkeeping')[0] if pd.read_html(main_url,match='Squad Advanced Goalkeeping') else None
squad_advanced_goalkeeping

我认为这段代码是解决方案,但我仍然得到"ValueError:没有找到匹配模式'小队高级守门员'">

您可以使用try-except块使用python异常处理

import pandas as pd
main_url='https://fbref.com/en/comps/9/2000-2001/2000-2001-Premier-League-Stats'
try:
squad_advanced_goalkeeping = pd.read_html(main_url,match='Squad Advanced Goalkeeping')[0] 
except ValueError as e:
squad_advanced_goalkeeping = None
print(squad_advanced_goalkeeping)

相关内容

  • 没有找到相关文章

最新更新