urllib获取到的内容转为 BeautifulSoup bs4对象后内容发生变化


http://samsung-updates.com/device/?id=SM-N9007
这个网址用linux curl和直接用urllib获取到的内容都是正常的,主要关注 table id="flist" 节点的内容如下:


 <table id="flist" border="0" width="100%" class="nosort">
<tr>
<td class="sorttable_nosort" colspan="2" width="13%"><center><b>Device Name</b></center></td>
<td width="80" class="sorttable_nosort"><b><center>Model</center></b></td>
<td width="20"><b><center>Region</center></b></td>
<td align="center" class="sorttable_nosort"><b><center>Version</center></b></td>
<td align="center"><b><center>OS</center></b></td>
<td align="center" class="sorttable_nosort"><b><center>OS Version</center></b></td>
<td align="center"><b><center>Build Date</center></b></td>
<td align="center"><b><center>Changelist</center></b></td>
<td align="center" class="sorttable_nosort"><b><center>Unlock</center></a></td>
<td align="center" class="sorttable_nosort"><b><center> </center></b></td>
</tr>
<tr>
<td align="left" colspan="2">Galaxy Note 3 (Snapdragon/SM-N9007)</td>
<td align="center"><a href="http://samsung-updates.com/device/?id=SM-N9007&details=Galaxy_Note_3__Snapdragon_and_SM-N9007">SM-N9007</a></td>
<td align="center"><a href="http://samsung-updates.com/region/?region=TGY" class="tooltip">TGY<span><b>Hong kong</b></span></a></td>
<td align="center">N9007ZHU3ANG2</td>
<td align="center"><img src="http://live.samsung-updates.com/44.png" border="0" width="32" height="22" alt="Android" title="KitKat" /></td>
<td align="center">4.4.2</td>
<td align="center">10.07.2014</td>
<td align="center">2188588</td>
<td align="center"><a href="http://www.unlocksamsungonline.com/samsung-SM-N9007-unlock-codes/">Unlock</a></td>
<td align="center">  <a href="/details/31397/Galaxy_Note_3_Snapdragon/SM-N9007/SM-N9007/TGY/N9007ZHU3ANG2.html" title="Download from Hotfile Mirror"><img src="http://samsung-updates.com/dbut.png" alt="Download File" width="90" height="35" /></a></td>
</tr>

但如果将获取到的内容转成bs4对象,获取的内容就不对了。请参见以下代码:


 import urllib.request
from bs4 import BeautifulSoup

def get_dom(page_url,encoding):
    opener = urllib.request.build_opener()
    opener.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11')]
    dom = opener.open(page_url)
    return dom.read().decode(encoding)
    dom.close()

def get_post_content(post_url):
    soup_post = BeautifulSoup(get_dom(post_url,"utf-8"))
    print(soup_post)

get_post_content("http://samsung-updates.com/device/?id=SM-N9007")

print出来的内容变成了:


 

<table border="0" class="nosort" id="flist" width="100%"> <tr> <td class="sorttable_nosort" colspan="2" width="13%"><center><b>Device Name</b></center></td> <td class="sorttable_nosort" width="80"><b><center>Model</center></b></td> <td width="20"><b><center>Region</center></b></td> <td align="center" class="sorttable_nosort"><b><center>Version</center></b></td> <td align="center"><b><center>OS</center></b></td> <td align="center" class="sorttable_nosort"><b><center>OS Version</center></b></td> <td align="center"><b><center>Build Date</center></b></td> <td align="center"><b><center>Changelist</center></b></td> <td align="center" class="sorttable_nosort"><b><center>Unlock</center></b></td></tr></table> </div></div></div></div> <td align="center" class="sorttable_nosort"><b><center> </center></b></td> <tr> <td align="left" colspan="2">Galaxy Note 3 (Snapdragon/SM-N9007)</td> <td align="center"><a href="http://samsung-updates.com/device/?id=SM-N9007&details=Galaxy_Note_3__Snapdragon_and_SM-N9007">SM-N9007</a></td> <td align="center"><a class="tooltip" href="http://samsung-updates.com/region/?region=TGY">TGY<span><b>Hong kong</b></span></a></td> <td align="center">N9007ZHU3ANG2</td> <td align="center"><img alt="Android" border="0" height="22" src="http://live.samsung-updates.com/44.png" title="KitKat" width="32"/></td> <td align="center">4.4.2</td> <td align="center">10.07.2014</td> <td align="center">2188588</td> <td align="center"><a href="http://www.unlocksamsungonline.com/samsung-SM-N9007-unlock-codes/">Unlock</a></td> <td align="center"> <a href="/details/31397/Galaxy_Note_3_Snapdragon/SM-N9007/SM-N9007/TGY/N9007ZHU3ANG2.html" title="Download from Hotfile Mirror"><img alt="Download File" height="35" src="http://samsung-updates.com/dbut.png" width="90"/></a></td> </tr>

请注意第一个tr节点后,table闭合了,还有有很多div闭合的标记。

这个问题导致我没法正确抓取表格的内容,求教下是不是我哪里有问题?

python beautifulsoup urllib

人形自走推妹机 10 years, 3 months ago

这是因为BeautifulSoup默认使用的HTML解析器(即标准库里的那个)对你的页面解析出错。解决方法是换一个解析器。

参见 http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html

P.S. 我用Python3.4,装了html5lib,并且在代码里用BeautifulSoup(xxx, 'html5lib')可以正确获得你想要的东西。

再P.S. 现在比较流行的html解析库是PyQuery,因为它的语法跟jQuery很像,很容易上手。而http库一般用requests,也是因为它提供的api很简单


 import requests
from pyquery import PyQuery as pq

def html(url):
    return requests.get(url).text


def table(text):
    return pq(text)('table#flist')


def table_from_url(url):
    return pq(url=url)('table#flist')


if __name__ == '__main__':
    print(table(html('http://samsung-updates.com/device/?id=SM-N9007')))
    # or
    print(table_from_url('http://samsung-updates.com/device/?id=SM-N9007'))

奔跑的小欣 answered 10 years, 3 months ago

Your Answer