用PHP或Python从HTML中提取数据

Nis*_*ant 1 php python

我需要提取这些数据并显示一个简单的图形.

Equity Share Capital这样的东西 - > 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>
Run Code Online (Sandbox Code Playgroud)

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

Hoo*_*ked 5

一个开始寻找的好地方是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
Run Code Online (Sandbox Code Playgroud)

这给出了:

[u'30.36', u'17.17', u'15.22', u'9.82', u'9.82']
Run Code Online (Sandbox Code Playgroud)

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

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

  • 如果您要求解决方案,您将不会自己学习如何使用它.当然,有人会给你解决方案,但尝试自己是最好的学习方式,用我的谦虚,也许是毫无价值的意见:-) (5认同)