我目前正在try 使用pandas和BeautifulSoup来创建html表,但在这样做时遇到了一个问题.

这里是url:https://ciffc.net/en/ciffc/ext/member/sitrep/

由于页面本质上是动态的,每天都会添加或删除表,因此不能使用pd数据帧的索引.也就是说,下面是我希望以今天的表索引7为例从表中提取的输出.

display(df[7].iloc[1,2])

>> 'Yukon is at a level 3 prep level - but will trend upwards with the forecasted hot and dry weather.'

我没有这个问题,因为我可以使用pandas的match参数.read\u html,但此表没有标题.表中包含的数据也是非常动态的,我唯一能够识别的唯一元素是"注释"列.以下是我try 识别此表的步骤:

APLtable = pd.read_html(url, match='Comments')[0].head(14)
display(APLtable)  

不幸的是,这不起作用,告诉我有以下错误

ValueError: No tables found matching pattern 'Comments'

我也try 过使用BeautifulSoup,但没有成功.考虑到网页的特殊性,我想知道是否有人知道如何引用该特定表格.

下面是有问题的html表:

</div></div><div id="section-apl" class="section-wrapper" data-title="E: Preparedness Levels"><div id="apl_table_wrapper"><table class="sticky-enabled">
 <thead><tr><th class="">Agency</th><th title="Agency Preparedness Level" class=" tooltip">APL</th><th class="">Comments</th> </tr></thead>
<tbody>
 <tr id="apl-table-row-0" class="odd"><td>BC</td><td>1</td><td></td> </tr>
 <tr id="apl-table-row-1" class="even"><td>YT</td><td>3</td><td>Yukon is at a level 3 prep level - but will trend upwards with the forecasted hot and dry weather.</td> </tr>
 <tr id="apl-table-row-2" class="odd"><td>AB</td><td>2</td><td></td> </tr>
 <tr id="apl-table-row-3" class="even"><td>SK</td><td>1</td><td></td> </tr>
 <tr id="apl-table-row-4" class="odd"><td>MB</td><td>1</td><td></td> </tr>
 <tr id="apl-table-row-5" class="even"><td>ON</td><td>1</td><td></td> </tr>
 <tr id="apl-table-row-6" class="odd"><td>QC</td><td>1</td><td></td> </tr>
 <tr id="apl-table-row-7" class="even"><td>NL</td><td>2</td><td></td> </tr>
 <tr id="apl-table-row-8" class="odd"><td>NB</td><td>1</td><td></td> </tr>
 <tr id="apl-table-row-9" class="even"><td>NS</td><td>1</td><td></td> </tr>
 <tr id="apl-table-row-10" class="odd"><td>PE</td><td>1</td><td></td> </tr>
 <tr id="apl-table-row-11" class="even"><td>PC</td><td>1</td><td></td> </tr>
</tbody>
</table>

推荐答案

这些表实际上是静态的,我想试试这个:

import requests
from bs4 import BeautifulSoup

import pandas as pd

headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:101.0) Gecko/20100101 Firefox/101.0",
}

soup = (
    BeautifulSoup(
        requests.get(
            "https://ciffc.net/en/ciffc/ext/member/sitrep/",
            headers=headers,
        ).text,
        "lxml",
    ).find("div", {"data-title": "E: Preparedness Levels"})
)

df = pd.read_html(str(soup), flavor="lxml")[0]
print(df)

这应始终输出:

   Agency  APL                                           Comments
0      BC    1                                                NaN
1      YT    3  Yukon is at a level 3 prep level - but will tr...
2      AB    2                                                NaN
3      SK    1                                                NaN
4      MB    1                                                NaN
5      ON    1                                                NaN
6      QC    1                                                NaN
7      NL    2                                                NaN
8      NB    1                                                NaN
9      NS    1                                                NaN
10     PE    1                                                NaN
11     PC    1                                                NaN

Python相关问答推荐

Pandas - groupby字符串字段并按时间范围 Select

从dict的列中分钟

用Python解密Java加密文件

Pandas—合并数据帧,在公共列上保留非空值,在另一列上保留平均值

当独立的网络调用不应该互相阻塞时,'

cv2.matchTemplate函数匹配失败

将JSON对象转换为Dataframe

Python脚本使用蓝牙运行在Windows 11与raspberry pi4

CommandeError:模块numba没有属性generated_jit''''

在Python 3中,如何让客户端打开一个套接字到服务器,发送一行JSON编码的数据,读回一行JSON编码的数据,然后继续?

如何创建引用列表并分配值的Systemrame列

通过追加列表以极向聚合

如何使用pytest在traceback中找到特定的异常

从列表中分离数据的最佳方式

大型稀疏CSR二进制矩阵乘法结果中的错误

牛郎星直方图中分类列的设置顺序

极地数据帧:ROLING_SUM向前看

如何计算二十面体每条三角形边的中点

在伪子进程中模拟标准输出.打开

如何省略所有Pandas 数据帧