Python使用Beautiful Soup包編寫(xiě)爬蟲(chóng)時(shí)的一些關(guān)鍵點(diǎn)
1.善于利用soup節(jié)點(diǎn)的parent屬性
比如對(duì)于已經(jīng)得到了如下html代碼:
<td style="padding-left:0" width="60%"><label>November</label> <input type="Hidden" id="cboMonth1" name="cboMonth1" value="11"> </td><td style="padding-right:0;" width="40%"> <label>2012</label> <input type="Hidden" id="cboYear1" name="cboYear1" value="2012"> </td>
的soup變量eachMonthHeader了。
想要提取其中的
Month的label的值:November
和Year的label的值:2012
最簡(jiǎn)單,也是最省事的辦法是,直接搜兩個(gè)label,然后肯定會(huì)找到這兩個(gè)label,然后分別對(duì)應(yīng)著Month和Year的label,然后獲得對(duì)應(yīng)的string即可:
foundTwoLabel = eachMonthHeader.findAll("label");
print "foundTwoLabel=",foundTwoLabel;
monthLabel = foundTwoLabel[0];
yearLabel = foundTwoLabel[1];
monthStr = monthLabel.string;
yearStr = yearLabel.string;
print "monthStr=",monthStr; # monthStr= November
print "yearStr=",yearStr; # yearStr= 2012
但是很明顯,這樣的邏輯性很不好,而且萬(wàn)一處理多個(gè)這樣的soup變量,而且兩者的順便顛倒了,那么結(jié)果也就錯(cuò)誤了。
此時(shí),可以考慮利用soup變量的parent屬性,從一個(gè)soup變量本身,獲得其上一級(jí)的soup變量。
示例代碼如下:
# <td style="padding-left:0" width="60%"><label>November</label>
# <input type="Hidden" id="cboMonth1" name="cboMonth1" value="11">
# </td><td style="padding-right:0;" width="40%">
# <label>2012</label>
# <input type="Hidden" id="cboYear1" name="cboYear1" value="2012">
# </td>
foundCboMonth = eachMonthHeader.find("input", {"id":re.compile("cboMonth\d+")});
#print "foundCboMonth=",foundCboMonth;
tdMonth = foundCboMonth.parent;
#print "tdMonth=",tdMonth;
tdMonthLabel = tdMonth.label;
#print "tdMonthLabel=",tdMonthLabel;
monthStr = tdMonthLabel.string;
print "monthStr=",monthStr;
foundCboYear = eachMonthHeader.find("input", {"id":re.compile("cboYear\d+")});
#print "foundCboYear=",foundCboYear;
tdYear = foundCboYear.parent;
#print "tdYear=",tdYear;
tdYearLabel = tdYear.label;
#print "tdYearLabel=",tdYearLabel;
yearStr = tdYearLabel.string;
print "yearStr=",yearStr;
我們?cè)賮?lái)看一個(gè)例子:
from BeautifulSoup import BeautifulSoup
doc = ['<html><head><title>Page title</title></head>',
'<body><p id="firstpara" align="center">This is paragraph <b>one</b>.',
'<p id="secondpara" align="blah">This is paragraph <b>two</b>.',
'</html>']
soup = BeautifulSoup(''.join(doc))
print soup.prettify()
# <html>
# <head>
# <title>
# Page title
# </title>
# </head>
# <body>
# <p id="firstpara" align="center">
# This is paragraph
# <b>
# one
# </b>
# .
# </p>
# <p id="secondpara" align="blah">
# This is paragraph
# <b>
# two
# </b>
# .
# </p>
# </body>
# </html>
這個(gè)例子中,<HEAD> Tag的parent是<HTML> Tag. <HTML> Tag 的parent是BeautifulSoup 剖析對(duì)象自己。 剖析對(duì)象的parent是None. 利用parent,你可以向前遍歷剖析樹(shù)。
soup.head.parent.name # u'html' soup.head.parent.parent.__class__.__name__ # 'BeautifulSoup' soup.parent == None # True
2.當(dāng)解析非UTF-8或ASCII編碼類(lèi)型的HTML時(shí),需要指定對(duì)應(yīng)的字符編碼
當(dāng)html為ASCII或UTF-8編碼時(shí),可以不指定html字符編碼,便可正確解析html為對(duì)應(yīng)的soup:
#這里respHtml是ASCII或UTF-8編碼,此時(shí)可以不指定編碼類(lèi)型,即可正確解析出對(duì)應(yīng)的soup soup = BeautifulSoup(respHtml);
當(dāng)html為其他類(lèi)型編碼,比如GB2312的話,則需要指定相應(yīng)的字符編碼,BeautifulSoup才能正確解析出對(duì)應(yīng)的soup:
比如:
#此處respHtml是GB2312編碼的,所以要指定該編碼類(lèi)型,BeautifulSoup才能解析出對(duì)應(yīng)的soup htmlCharset = "GB2312"; soup = BeautifulSoup(respHtml, fromEncoding=htmlCharset);
相關(guān)文章
Python爬蟲(chóng)之網(wǎng)頁(yè)圖片抓取的方法
最近小編一直在學(xué)習(xí)python的東西,今天小編給大家分享基于python寫(xiě)的一個(gè)爬蟲(chóng)程序,能實(shí)現(xiàn)簡(jiǎn)單的網(wǎng)頁(yè)圖片下載,具體實(shí)例代碼大家參考下本文2018-07-07
python?tkinter自定義實(shí)現(xiàn)Expander控件
和其他成熟的GUI庫(kù)相比,tkinter的組件并不是太多,但在自定義組件這一點(diǎn)上,并不遜色于其他框架,下面小編就教大家如何自定義一個(gè)Expander控件吧2023-08-08
Python實(shí)現(xiàn)GUI學(xué)生信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)GUI學(xué)生信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Python 爬蟲(chóng)學(xué)習(xí)筆記之正則表達(dá)式
正則表達(dá)式是用來(lái)匹配字符串非常強(qiáng)大的工具,在其他編程語(yǔ)言中同樣有正則表達(dá)式的概念,Python同樣不例外,利用了正則表達(dá)式,我們想要從返回的頁(yè)面內(nèi)容提取出我們想要的內(nèi)容就易如反掌了。2016-09-09
Python時(shí)間序列缺失值的處理方法(日期缺失填充)
這篇文章主要給大家介紹了關(guān)于Python時(shí)間序列缺失值(日期缺失填充)的處理方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Python解壓可迭代對(duì)象賦值給多個(gè)變量詳解
這篇文章主要為大家介紹了Python賦值多個(gè)變量,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2021-12-12
pyinstaller還原python代碼過(guò)程圖解
這篇文章主要介紹了pyinstaller還原python代碼過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
python列表推導(dǎo)式入門(mén)學(xué)習(xí)解析
這篇文章主要介紹了python列表推導(dǎo)式入門(mén)學(xué)習(xí)解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12

