首页 > 解决方案 > html_read:只读数据,而不是表格的形状 [A 行 x B 列] (Python)

问题描述

我正在使用 pd.read_html 在循环中从网站上抓取数据Python 3.7,并且很难将其导出。

html字符串的相关部分:

html_source =

<div class="reiterZwischenzeile">
    &nbsp;
</div>
<table class="tabelleOhneWidth" width="100%" cellspacing="0px">
    <colgroup>
        <col class="left" width="300px" valign="middle">
        <col class="left" width="80px" valign="middle">
        <col class="left" width="80px" valign="middle">
        <col class="left" width="80px" valign="middle">
        <col class="left" width="80px" valign="middle">
        <col class="left" width="20px" valign="middle">
        <col class="left" width="80px" valign="middle">
        <col class="left" width="80px" valign="middle">
        <col class="left" width="20px" valign="middle">
        <col class="left" width="20px" valign="middle">
    </colgroup>
    <tbody><tr>
        <td class="tabelleKopfUo left" colspan="2" rowspan="2">
            Teilarbeit
        </td>
        <td class="tabelleKopfUo center" rowspan="2">
            Arbeitszeit-<br>bedarf
        </td>
        <td class="tabelleKopfUo center" rowspan="2">
            Flächen-<br>leistung
        </td>
        <td class="tabelleKopfUo center" colspan="5">
            Maschinenkosten
        </td>
        <td class="tabelleKopfUo center" rowspan="2">
            Diesel-<br>bedarf
        </td>
    </tr>
    <tr>
        <td class="tabelleKopfOoUo center">
            Abschreibung
        </td>
        <td class="tabelleKopfOoUo center">
            Zinskosten
        </td>
        <td class="tabelleKopfOoUo center">
            Sonstiges&nbsp;<img src="images/info_white_10.png" border="none"> 
        </td>
        <td class="tabelleKopfOoUo center">
            Reparaturen
        </td>
        <td class="tabelleKopfOoUo center">
            Betriebsstoffe
        </td>
    </tr>
    <tr>
        <td class="tabelleKopfOo center" colspan="2"></td>
        <td class="tabelleKopfOo center">
            Akh/ha
        </td>
        <td class="tabelleKopfOo center">
            ha/h
        </td>
        <td class="tabelleKopfOo center" colspan="5">
            €/ha
        </td>
        <td class="tabelleKopfOo center" colspan="5">
            l/ha
        </td>
    </tr>

        <tr>
            <td class="tabelleEbene2  left">
                2.000 l, Aufbaupflanzenschutzspritze; 138 kW
            </td>
            <td class="tabelleEbene2  right">
                Feldarbeit
            </td>
            <td class="tabelleEbene2  right">
                0.11
            </td>
            <td class="tabelleEbene2  right">
                9.09
            </td>
            <td class="tabelleEbene2  right">
                3.72
            </td>
            <td class="tabelleEbene2  right">
                0.91
            </td>
            <td class="tabelleEbene2  right">
                0.24
            </td>
            <td class="tabelleEbene2  right">
                1.59
            </td>
            <td class="tabelleEbene2  right">
                0.68
            </td>
            <td class="tabelleEbene2  right">
                0.90
            </td>
        </tr>



</tbody></table>




然后我在每次迭代中读取 html 表,如下所示:


        df_list = pd.read_html(html_source, skiprows = [0,1,2])


打印 df_list 给了我这个(索引 df_list[0] 也无济于事):

print(df_list)

[                                             0           1     2   ...  11  12  13
0  2.000 l, Aufbaupflanzenschutzspritze; 138 kW  Feldarbeit  0.11  ...            

[1 rows x 14 columns]]

我用这样一个简单的html代码尝试了同样的方法:

<html>

<body>


<table><tr></tr></table>
<table><tr></tr></table>


blablabal
blabalalb
slkjflsjbs
sjflsbsb


Table1
<table border=1>
<tr>
<td>Test1</td><td>3</td><td>6</td><td>8.8</td><td>Test</td>
</tr>
<tr>
</tr>
<td>4</td><td>7</td><td>8</td><td>88</td><td>Test</td>
<td>74</td><td>77</td><td>78</td><td>88</td><td>Test</td><td>74</td><td>77</td><td>78</td><td>88</td><td>Test</td>
</table>


</body>

<html>


htmlname = r"example.html"
html = open(htmlname, 'r')

source_code = html.read()
#print(source_code)
tables = pd.read_html(source_code, skiprows=[1])

print(tables)

[       0  1  2    3     4
0  Test1  3  6  8.8  Test]
>>>

为什么我从网站上阅读时会得到这个形状描述,我怎样才能摆脱它?

标签: pythonweb-scrapinghtml-table

解决方案


尝试使用此选项:-

pd.options.display.show_dimensions = False
df_list = pd.read_html(html_source,skiprows=3)
print(df_list)

还只是为了回答为什么它显示第一个 html 源的维度是,对于 pandas 较新版本,对于适合控制台的小型数据框,维度不会显示。它们仅在数据帧输出较大时显示。示例:-在您的情况下

df = pd.concat(df_list)
df1 = df[df.columns[range(4)]]
df1

如果您从 df_list 中仅选择 4 列,则由于第 4 列的数量少于 14 列,因此它不会显示维度。


推荐阅读