国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python使用Beautiful Soup包編寫爬蟲時的一些關鍵點

2020-01-04 17:49:46
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了Python使用Beautiful Soup包編寫爬蟲時的一些關鍵點,文中講到了parent屬性的使用以及soup的編碼問題,需要的朋友可以參考下
 

1.善于利用soup節點的parent屬性

比如對于已經得到了如下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,然后分別對應著Month和Year的label,然后獲得對應的string即可:

foundTwoLabel = eachMonthHeader.findAll("label");print "foundTwoLabel=",foundTwoLabel;monthLabel = foundTwoLabel[0];yearLabel = foundTwoLabel[1]; monthStr = monthLabel.string;yearStr = yearLabel.string; print "monthStr=",monthStr; # monthStr= Novemberprint "yearStr=",yearStr; # yearStr= 2012

但是很明顯,這樣的邏輯性很不好,而且萬一處理多個這樣的soup變量,而且兩者的順便顛倒了,那么結果也就錯誤了。

此時,可以考慮利用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.當解析非UTF-8或ASCII編碼類型的HTML時,需要指定對應的字符編碼

當html為ASCII或UTF-8編碼時,可以不指定html字符編碼,便可正確解析html為對應的soup:

#這里respHtml是ASCII或UTF-8編碼,此時可以不指定編碼類型,即可正確解析出對應的soupsoup = BeautifulSoup(respHtml);

當html為其他類型編碼,比如GB2312的話,則需要指定相應的字符編碼,BeautifulSoup才能正確解析出對應的soup:

比如:

#此處respHtml是GB2312編碼的,所以要指定該編碼類型,BeautifulSoup才能解析出對應的souphtmlCharset = "GB2312";soup = BeautifulSoup(respHtml, fromEncoding=htmlCharset);
 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 海林市| 陕西省| 许昌县| 泗洪县| 龙井市| 监利县| 穆棱市| 卢氏县| 丰顺县| 陆良县| 九龙县| 仪征市| 铜鼓县| 滦平县| 裕民县| 九龙坡区| 固始县| 桐城市| 元阳县| 全椒县| 普宁市| 南京市| 威远县| 天水市| 建平县| 洞口县| 墨竹工卡县| 甘泉县| 盐边县| 宝兴县| 丽水市| 通化县| 皋兰县| 永康市| 望江县| 通江县| 波密县| 中超| 锡林郭勒盟| 盈江县| 宜良县|