Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.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 使用pd.read_html解析html表,其中单元格本身包含完整的表_Python_Html_Pandas_Beautifulsoup_Lxml - Fatal编程技术网

Python 使用pd.read_html解析html表,其中单元格本身包含完整的表

Python 使用pd.read_html解析html表,其中单元格本身包含完整的表,python,html,pandas,beautifulsoup,lxml,Python,Html,Pandas,Beautifulsoup,Lxml,我需要从html中解析一个表,该表在较大的表中嵌套了其他表。正如下面用pd.read_html调用的,这些嵌套表中的每一个都被解析,然后作为行“插入”/“连接” 我希望将这些嵌套表解析为各自的pd.DataFrames,并将其作为对象插入为相应列的值 如果这是不可能的,那么将嵌套表的原始html作为字符串放在相应的位置就可以了 测试代码: 将熊猫作为pd导入 df_up=pd.read_html(“up_pf00344.test.html”,attrs={'id':'results'}) 输出

我需要从html中解析一个表,该表在较大的表中嵌套了其他表。正如下面用
pd.read_html
调用的,这些嵌套表中的每一个都被解析,然后作为行“插入”/“连接”

我希望将这些嵌套表解析为各自的
pd.DataFrames
,并将其作为对象插入为相应列的值

如果这是不可能的,那么将嵌套表的原始html作为字符串放在相应的位置就可以了

测试代码:

将熊猫作为pd导入
df_up=pd.read_html(“up_pf00344.test.html”,attrs={'id':'results'})
输出屏幕截图:

以html格式呈现的表格屏幕截图:

链接到文件:

您不能用于读取嵌套表,但您可以滚动自己的html阅读器,并使用
read\u html
读取表格单元格:

import pandas as pd
import bs4

with open('up_pf00344.test.html') as f:
    html = f.read()
soup = bs4.BeautifulSoup(html, 'lxml')
results = soup.find(attrs = {'id': 'results'})

# get first visible header row as dataframe headers
for row in results.thead.find_all('tr'):
    if 'display:none' not in row.get('style',''):
        df = pd.DataFrame(columns=[col.get_text() for col in row.find_all('th')])
    break

# append all table rows to dataframe
for row in results.tbody.find_all('tr', recursive=False):
    if 'display:none' in row.get('style',''):
        continue
    df_row = []
    for col in row.find_all('td', recursive=False):
        table = col.find_all('table')
        df_row.append(pd.read_html(str(col))[0] if table else col.get_text())
    df.loc[len(df)] = df_row
df.iloc[0]的结果。映射(类型)


进入
有机体
蛋白质名称
基因名
长度
交叉参考(Pfam)
交叉参考(InterPro)
分类谱系ID
亚细胞定位
信号肽
转运肽
拓扑域
跨膜
膜内
顺序警告
小心
分类谱系(超级王国)
分类谱系(王国)
分类谱系(门)
交叉参考(参考序号)
交叉参考(EMBL)
E
另外:由于您的表行有一个
id
,您可以使用它作为数据帧的索引
df.loc[row.get('id')]=df\u row
,而不是
df.loc[len(df)]=df\u row

                                                            <class 'str'>
Entry                                                       <class 'str'>
Organism                                                    <class 'str'>
Protein names                                               <class 'str'>
Gene names                                                  <class 'str'>
Length                                                      <class 'str'>
Cross-reference (Pfam)                                      <class 'str'>
Cross-reference (InterPro)                                  <class 'str'>
Taxonomic lineage IDs                                       <class 'str'>
Subcellular location [CC]                                   <class 'str'>
Signal peptide                                              <class 'str'>
Transit peptide                                             <class 'str'>
Topological domain                  <class 'pandas.core.frame.DataFrame'>
Transmembrane                       <class 'pandas.core.frame.DataFrame'>
Intramembrane                       <class 'pandas.core.frame.DataFrame'>
Sequence caution                                            <class 'str'>
Caution                                                     <class 'str'>
Taxonomic lineage (SUPERKINGDOM)                            <class 'str'>
Taxonomic lineage (KINGDOM)                                 <class 'str'>
Taxonomic lineage (PHYLUM)                                  <class 'str'>
Cross-reference (RefSeq)                                    <class 'str'>
Cross-reference (EMBL)                                      <class 'str'>
e                                                           <class 'str'>