Python使用Beautiful Soup包編寫爬蟲時的一些關(guān)鍵點
1.善于利用soup節(jié)點的parent屬性
比如對于已經(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
最簡單,也是最省事的辦法是,直接搜兩個label,然后肯定會找到這兩個label,然后分別對應(yīng)著Month和Year的label,然后獲得對應(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
但是很明顯,這樣的邏輯性很不好,而且萬一處理多個這樣的soup變量,而且兩者的順便顛倒了,那么結(jié)果也就錯誤了。
此時,可以考慮利用soup變量的parent屬性,從一個soup變量本身,獲得其上一級的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;
我們再來看一個例子:
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>
這個例子中,<HEAD> Tag的parent是<HTML> Tag. <HTML> Tag 的parent是BeautifulSoup 剖析對象自己。 剖析對象的parent是None. 利用parent,你可以向前遍歷剖析樹。
soup.head.parent.name # u'html' soup.head.parent.parent.__class__.__name__ # 'BeautifulSoup' soup.parent == None # True
2.當(dāng)解析非UTF-8或ASCII編碼類型的HTML時,需要指定對應(yīng)的字符編碼
當(dāng)html為ASCII或UTF-8編碼時,可以不指定html字符編碼,便可正確解析html為對應(yīng)的soup:
#這里respHtml是ASCII或UTF-8編碼,此時可以不指定編碼類型,即可正確解析出對應(yīng)的soup soup = BeautifulSoup(respHtml);
當(dāng)html為其他類型編碼,比如GB2312的話,則需要指定相應(yīng)的字符編碼,BeautifulSoup才能正確解析出對應(yīng)的soup:
比如:
#此處respHtml是GB2312編碼的,所以要指定該編碼類型,BeautifulSoup才能解析出對應(yīng)的soup htmlCharset = "GB2312"; soup = BeautifulSoup(respHtml, fromEncoding=htmlCharset);
相關(guān)文章
python類參數(shù)定義及數(shù)據(jù)擴展方式unsqueeze/expand
本文主要介紹了python類參數(shù)定義及數(shù)據(jù)擴展方式unsqueeze/expand,文章通過圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-08-08Pygame坦克大戰(zhàn)游戲開發(fā)實戰(zhàn)詳解代碼
《坦克大戰(zhàn)》以二戰(zhàn)坦克為題材,既保留了射擊類游戲的操作性,也改進了射擊類游戲太過于復(fù)雜難玩的高門檻特點,集休閑與競技于一身。經(jīng)典再度襲來,流暢的畫面,瘋狂的戰(zhàn)斗,讓玩家再次進入瘋狂坦克的世界。玩家的目標(biāo)是控制坦克躲避危險,消滅掉所有的敵人即可進入下一關(guān)2022-02-02python使用numpy尋找二維數(shù)組的最值及其下標(biāo)方法分析
這篇文章主要為大家介紹了python使用numpy尋找二維數(shù)組的最值及其下標(biāo)實現(xiàn)方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08python Requsets下載開源網(wǎng)站的代碼(帶索引 數(shù)據(jù))
這篇文章主要介紹了python Requsets下載開源網(wǎng)站的代碼(帶索引 數(shù)據(jù)),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04Python如何使用神經(jīng)網(wǎng)絡(luò)進行簡單文本分類
這篇文章主要介紹了Python如何使用神經(jīng)網(wǎng)絡(luò)進行簡單文本分類,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-02-02