如何使用python和beautifulsoup从嵌入在html中的excel表中提取数据?

时间:2013-11-21 07:13:00

标签: python excel beautifulsoup

所以我想到了从网页上的表中提取数据的想法,这样我就可以对其进行平均,直观地表示并使用它。我已经尝试使用python和beautifulsoup来获取数据,但我仍然最终得到了这个看起来像这样的奇怪的excel格式代码:

<!--table
    {mso-displayed-decimal-separator:"\.";
    mso-displayed-thousand-separator:"\,";}
@page
    {margin:1.0in .75in 1.0in .75in;
    mso-header-margin:.51in;
    mso-footer-margin:.51in;}
.style0
    {mso-number-format:General;
    text-align:general;
    vertical-align:bottom;
    white-space:nowrap;
    mso-rotate:0;
    mso-background-source:auto;
...(more of the same)
...

-->

我查看了该页面的源代码,其中包括:

<meta name=ProgId content=Excel.Sheet>
<meta name=Generator content="Microsoft Excel 14">

如何以有意义的方式提取数据,保留数据并允许对其进行操作?谢谢你的时间。

我当前的脚本只使用curl来获取html文件,然后打开html文件并在其上使用beautifulsoup get_text,并将其保存到文本文件中。

1 个答案:

答案 0 :(得分:0)

你在做这样的事吗?:

 import BeautifulSoup
 s = BeautifulSoup.BeautifulSoup(html)
 table = s.find("table", {"id": "mytableid"})
 try:
     rows = table.findAll('tr')
     for tr in rows:
         cols = tr.findAll('td')
         for td in cols:
             val = td.text

在你提出问题之前,我无法给你一个更好的答案。