Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正确读取html表格_Python_Html_Pandas - Fatal编程技术网

Python 正确读取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

这是一个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 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中有效。我将为