我试图解析在http://www.swiftcodesbic.com找到的表,并使用Pandas自动获取表。在大多数情况下,这是很好的工作,但有一个表中有两个
标记,我认为这导致了一个问题。故障表可以找到here。在我用来将html解析为pandas.DataFrame的代码是:
pandas.read_html(countryPage.text, attrs={"id":"t2"}, skiprows=1)[0]
其中countryPage是requests.get()对象。有什么我可以添加到pandas调用中以告诉它获取第二个
标记吗?或者,如果这不是问题所在,有人能解释一下是什么原因导致它返回“table not found”错误吗?提前谢谢。在编辑
这是我目前使用的解决方案,但我还是想知道一个更“Python”的方法。在try:
tempDataFrame = pd.read_html(countryPage.text, attrs={"id":"t2"}, skiprows=1)[0]
except:
if "france" is in url: #pseudo-code
soup = BeautifulSoup(countryPage.text)
table = soup.find_all("table")[2].findAll('tbody')[1] #this will vary based on your situation
table = "
" #pandas needs the table tag to recognize a tabletempDataFrame = pd.read_html(table)[0]
再说一次,我很想知道如何更有效地完成这项工作。在