[英]How to read an html table with multiple tbodies with python pandas' read_html?
This is my html: 这是我的html:
import pandas as pd
html_table = '''<table>
<thead>
<tr><th>Col1</th><th>Col2</th>
</thead>
<tbody>
<tr><td>1a</td><td>2a</td></tr>
</tbody>
<tbody>
<tr><td>1b</td><td>2b</td></tr>
</tbody>
</table>'''
If I run df = pd.read_html(html_table)
, and then print(df[0]
I get: 如果我运行
df = pd.read_html(html_table)
,然后运行print(df[0]
我得到:
Col1 Col2
0 1a 2a
Col 2 disappears. 第2列消失。 Why?
为什么? How to prevent it?
怎么预防呢?
The HTML you have posted is not a valid one . 您发布的HTML无效 。 Multiple
tbody
s is what confuses the pandas
parser logic. 多个
tbody
混淆了pandas
解析器逻辑。 If you cannot fix the input html itself, you have to pre-parse it and "unwrap" all the tbody
elements: 如果您无法修复输入html本身,则必须预先对其进行解析并“解包”所有
tbody
元素:
import pandas as pd
from bs4 import BeautifulSoup
html_table = '''
<table>
<thead>
<tr><th>Col1</th><th>Col2</th>
</thead>
<tbody>
<tr><td>1a</td><td>2a</td></tr>
</tbody>
<tbody>
<tr><td>1b</td><td>2b</td></tr>
</tbody>
</table>'''
# fix HTML
soup = BeautifulSoup(html_table, "html.parser")
for body in soup("tbody"):
body.unwrap()
df = pd.read_html(str(soup), flavor="bs4")
print(df[0])
Prints: 打印:
Col1 Col2
0 1a 2a
1 1b 2b
Having multiple tbody tag causes the problem when pd.read_html() is called. 当调用pd.read_html()时,具有多个tbody标签会引起问题。 Having multiple tbody tag is legal in html5 and can be convenient for styling but it looks like it is not supported by pd.read_html().
在HTML5中,具有多个tbody标签是合法的,可以方便地进行样式设置,但pd.read_html()似乎不支持它。 But if you can just use single
<tbody>
it just works fine. 但是,如果您仅可以使用单个
<tbody>
则效果很好。
html_table1 = '''<table>
<thead>
<tr><th>Col1</th><th>Col2</th>
</thead>
<tbody>
<tr><td>1a</td><td>2a</td></tr>
<tr><td>1b</td><td>2b</td></tr>
</tbody>
</table>'''
df1 = pd.read_html(html_table1)
print(df1)
[ Col1 Col2
0 1a 2a
1 1b 2b]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.