gpt4 book ai didi

php - 在 PHP 或 Python 中从 HTML 中提取数据

转载 作者:太空宇宙 更新时间:2023-11-04 07:12:27 26 4
gpt4 key购买 nike

我需要提取这些数据并从中显示一个简单的图表。

类似股权资本的东西 -> array (30.36, 17, 17 .... etc) 会有所帮助。

<html:tr>
<html:td>Equity Share Capital</html:td>
<html:td class="numericalColumn">30.36</html:td>
<html:td class="numericalColumn">17.17</html:td>
<html:td class="numericalColumn">15.22</html:td>
<html:td class="numericalColumn">9.82</html:td>
<html:td class="numericalColumn">9.82</html:td>
</html:tr>

如何在 PHP 或 Python 中完成此任务?

最佳答案

开始寻找的好地方是 python 模块 BeautifulSoup它提取文本并将其放入表格中。

假设您已将数据加载到名为 raw 的变量中:

from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(raw)

for x in soup.findAll("html:td"):
if x.string == "Equity share capital":
VALS = [y.string for y in x.parent.findAll() if y.has_key("class")]

print VALS

这给出:

[u'30.36', u'17.17', u'15.22', u'9.82', u'9.82']

您会注意到这是一个 unicode 字符串列表,请确保在处理之前将它们转换为您想要的任何类型。

通过 BeautifulSoup 有很多方法可以做到这一点。然而,我发现的好处是,快速破解通常足以 (TM) 完成工作!

关于php - 在 PHP 或 Python 中从 HTML 中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4484985/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com