Python 正确读取html表格
这是一个HTML表格:Python 正确读取html表格,python,html,pandas,Python,Html,Pandas,这是一个HTML表格: A B C D E F G H 我 J K L M 平方米 N 氮气 O 氧气 P P2 您可以使用BeautifulSoup解析表格,然后将结果转换为数据帧: import pandas as pd from bs4 import BeautifulSoup as soup df = pd.DataFrame([[k[1:-1] for i in b.find_all('td') if (k:=i.text) is not N
A
B
C
D
E
F
G
H
我
J
K
L
M
平方米
N
氮气
O
氧气
P
P2
您可以使用BeautifulSoup
解析表格,然后将结果转换为数据帧:
import pandas as pd
from bs4 import BeautifulSoup as soup
df = pd.DataFrame([[k[1:-1] for i in b.find_all('td') if (k:=i.text) is not None] for b in soup(html, 'html.parser').table.find_all('tr')])
输出:
0 1 2 3 4 5 6 7
0 A B C D None None None None
1 E F G H None None None None
2 I J K L None None None None
3 M M2 N N2 O O2 P P2
0 1 2 3 4 5 6 7
0 A B C D None None None None
1 E F G H None None None None
2 I J K L None None None None
3 M M2 N N2 O O2 P P2
编辑:不带赋值表达式的解决方案:
df = pd.DataFrame([[i.text[1:-1] if i else i for i in b.find_all('td')] for b in soup(html, 'html.parser').table.find_all('tr')])
输出:
0 1 2 3 4 5 6 7
0 A B C D None None None None
1 E F G H None None None None
2 I J K L None None None None
3 M M2 N N2 O O2 P P2
0 1 2 3 4 5 6 7
0 A B C D None None None None
1 E F G H None None None None
2 I J K L None None None None
3 M M2 N N2 O O2 P P2
(k:=i.text)
@MichaelO附近的“无效语法”。这是一个赋值表达式,仅在Python 3.8中有效。我将为