使用BeautifulSoup解析一个长html失败,半解析输出

时间:2014-08-19 02:16:22

标签: python html-parsing beautifulsoup

我使用以下脚本来解析特定基金的基金价格:

import pandas as pd
from bs4 import BeautifulSoup
from ghost import Ghost
ghost = Ghost()
page,resources = ghost.open('http://bank.hangseng.com/1/PA_1_1_P1/ComSvlet_MiniSite_eng_gif?app=eINVCFundDetailsOV&pri_fund_code=U44217')
page,resources = ghost.evaluate("agree()", expect_loading=True)
page,resources = ghost.evaluate("MM_changeview('eINVCFundPriceDividend')", expect_loading=True)
# ghost.capture_to("hangseng.png")
soup = BeautifulSoup(page.content)
soup

输出soup对于前半部分是正常的,但标签全部都是大写的,而BeautifulSoup无法解析它们,如下所示:

<td class="LightGrey" valign="TOP"><font class="CONTENT">22-07-2014</font></td><td class="LightGrey" valign="TOP"><font class="CONTENT">10.95000</font></td><td class="LightGrey" valign="TOP"><font class="CONTENT">11.39000</font></td><td class="LightGrey" valign="TOP"><font class="CONTENT">10.95000</font></td>
</tr>
 T R   V A L I G N = " t o p "   a l i g n = " c e n t e r " &gt; 
 T D   C L A S S = " L i g h t G r e y "   V A L I G N = " T O P " &gt; F O N T   C L A S S = " C O N T E N T " &gt; 2 1 - 0 7 - 2 0 1 4 / F O N T &gt; / T D &gt; T D   C L A S S = " L i g h t G r e y "   V A L I G N = " T O P " &gt; F O N T   C L A S S = " C O N T E N T " &gt; 1 0 . 9 6 0 0 0 / F O N T &gt; / T D &gt; T D   C L A S S = " L i g h t G r e y "   V A L I G N = " T O P " &gt; F O N T   C L A S S = " C O N T E N T " &gt; 1 1 . 4 0 0 0 0 / F O N T &gt; / T D &gt; T D   C L A S S = " L i g h t G r e y "   V A L I G N = " T O P " &gt; F O N T   C L A S S = " C O N T E N T " &gt; 1 0 . 9 6 0 0 0 / F O N T &gt; / T D &gt; 
 / T R &gt; 

您可以在日期2014-07-22之后看到输出变为垃圾。

发生了什么事?

1 个答案:

答案 0 :(得分:2)

我找到了spaced output beautifulsoup

的解决方案
page.content
soup = BeautifulSoup(page.content,'html.parser')

现在它完美无缺。