我使用以下脚本来解析特定基金的基金价格:
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 " >
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 " > F O N T C L A S S = " C O N T E N T " > 2 1 - 0 7 - 2 0 1 4 / F O N T > / T D > 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 " > F O N T C L A S S = " C O N T E N T " > 1 0 . 9 6 0 0 0 / F O N T > / T D > 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 " > F O N T C L A S S = " C O N T E N T " > 1 1 . 4 0 0 0 0 / F O N T > / T D > 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 " > F O N T C L A S S = " C O N T E N T " > 1 0 . 9 6 0 0 0 / F O N T > / T D >
/ T R >
您可以在日期2014-07-22
之后看到输出变为垃圾。
发生了什么事?
答案 0 :(得分:2)
我找到了spaced output beautifulsoup
的解决方案page.content
soup = BeautifulSoup(page.content,'html.parser')
现在它完美无缺。