python爬蟲:使用BeautifulSoup修改網頁內容

 

 

 

 

 

BeautifulSoup除了能夠查找和定位網頁內容,還能夠修改網頁。修改意味着能夠增長或刪除標籤,改變標籤名字,變動標籤屬性,改變文本內容等等。html

 使用修BeautifulSoup修改標籤

每個標籤在BeautifulSoup裏面都被看成一個標籤對象,這個對象能夠執行如下任務:python

  • 修改標籤名
  • 修改標籤屬性
  • 增長新標籤
  • 刪除存在的標籤
  • 修改標籤的文本內容

修改標籤的名字

只須要修改.name參數就能夠修改標籤名字。app

  1. producer_entries.name = "div"<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">怎麼辦嘛</span><img src="file:///C:\Users\ADMINI~1\AppData\Local\Temp\~LWHD)}S}%DE5RTOO[CVEI1.gif" sysface="15" style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);" alt="" />  
你咋這麼說 


修改標籤的屬性

修改標籤的屬性如class,id,style等。由於屬性以字典形式儲存,因此改變標籤屬性就是簡單的處理Python的字典。函數

更新已經存在屬性的標籤

能夠參照以下代碼:spa

  1. producer_entries['id']="producers_new_value"  

 

爲一個標籤增長一個新的屬性

好比一個標籤沒有class屬性,那麼能夠參照以下代碼增長class屬性,.net

  1. producer_entries['class']='newclass'  

 

刪除標籤屬性

使用del操做符,示例以下:xml

  1. del producer_entries['class']  

 

增長一個新的標籤

BeautifulSoup有new_tag()方法來創造一個新的標籤。而後可使用append(),insert(),insert_after()或者insert_before()等方法來對新標籤進行插入。htm

增長一個新生產者,使用new_tag()而後append()對象

參照前面例子,生產者除了plants和alage外,咱們如今添加一個phytoplankton.首先,須要先創造一個li標籤。blog

用new_tag()建立一個新標籤

new_tag()方法只能用於BeautifulSoup對象。如今建立一個li對象。

  1. soup = BeautifulSoup(html_markup,"lxml")  
  2. new_li_tag = soup.new_tag("li")  


new_tag()對象必須的參數是標籤名,其餘標籤屬性參數或其餘參數都是可選參數。舉例:

  1. new_atag=soup.new_tag("a",href="www.example.com")  

 

  1. new_li_tag.attrs={'class':'producerlist'}  


使用append()方法添加新標籤

append()方法添加新標籤於,contents以後,就跟python列表方法append()同樣。

  1. producer_entries = soup.ul  
  2. producer_entries.append(new_li_tag)  


li標籤是ul標籤的子代,添加新標籤後的輸出結果。

<ul id="producers">
<li class="producerlist">
<div class="name">
plants
</div>
<div class="number">
100000
</div>
</li>
<li class="producerlist">
<div class="name">
algae
</div>
<div class="number">
100000
</div>
</li>s
<li class="producerlist">
</li>

</ul>

使用insert()向li標籤中添加新的div標籤

append()在.contents以後添加新標籤,而insert()卻不是如此。咱們須要指定插入的位置。就跟python中的Insert()方法同樣。

  1. new_div_name_tag=soup.new_tag("div")  
  2. new_div_name_tag["class"]="name"  
  3. new_div_number_tag=soup.new_tag("div")  
  4. new_div_number_tag["class"]="number"  


先是建立兩個div標籤

  1. new_li_tag.insert(0,new_div_name_tag)  
  2. new_li_tag.insert(1,new_div_number_tag)  
  3. print(new_li_tag.prettify())  


而後進行插入,輸出效果以下:

<li class_="producerlist">
<div class="name">
</div>
<div class="number">
</div>

</li>

改變字符串內容

在上面例子中,只是添加了標籤,但標籤中卻沒有內容,若是想添加內容的話,BeautifulSoup也能夠作到。

使用.string修改字符串內容

好比:

  1. new_div_name_tag.string="phytoplankton"  
  2. print(producer_entries.prettify())  

輸出以下:

<ul id="producers">
<li class="producerlist">
<div class="name">
plants
</div>
<div class="number">
100000
</div>
</li>
<li class="producerlist">
<div class="name">
algae
</div>
<div class="number">
100000
</div>
</li>
<li class="producerlist">
<div class="name">
phytoplankton
</div>

<div class="number">
</div>
</li>
</ul>

使用.append/(),insert(),和new_string()添加字符串

使用append()和insert()的效果就跟用在添加新標籤中同樣。好比:

  1. new_div_name_tag.append("producer")  
  2. print(soup.prettify())  


輸出:

  1. <html>  
  2. <body>  
  3. <div class="ecopyramid">  
  4. <ul id="producers">  
  5. <li class="producerlist">  
  6. <div class="name">  
  7. plants  
  8. </div>  
  9. <div class="number">  
  10. 100000  
  11. </div>  
  12. </li>  
  13. <li class="producerlist">  
  14. <div class="name">  
  15. algae  
  16. </div>  
  17. <div class="number">  
  18. 100000  
  19. </div>  
  20. </li>  
  21. <li class="producerlist">  
  22. <strong><div class="name">  
  23. phytoplankton  
  24. producer  
  25. </div>  
  26. </strong><div class="number">  
  27. </div>  
  28. </li>  
  29. </ul>  
  30. </div>  
  31. </body>  
  32. </html>  


還有一個new_string()方法,

  1. new_string_toappend = soup.new_string("producer")  
  2. new_div_name_tag.append(new_string_toappend)  

 

從網頁中刪除一個標籤

刪除標籤的方法有decomose()和extract()方法


使用decompose()刪除生產者


咱們如今移去class="name"屬性的div標籤,使用decompose()方法。

  1. third_producer = soup.find_all("li")[2]  
  2. div_name = third_producer.div  
  3. div_name.decompose()  
  4. print(third_producer.prettify())  


輸出:

<li class_="producerlist">
<div class_="number">
10000
</div>

</li>

decompose()方法會移去標籤及標籤的子代。

使用extract()刪除生產者

extract()用於刪除一個HTMNL文檔中昂的標籤或者字符串,另外,它還返回一個被刪除掉的標籤或字符串的句柄。不一樣於decompose(),extract也能夠用於字符串。

  1. third_producer_removed=third_producer.extract()  
  2. print(soup.prettify())  


使用BeautifulSoup刪除標籤的內容

標籤能夠有一個NavigableString對象或tag對象做爲子代。刪除掉這些子代可使用clear()

舉例,能夠移掉帶有plants的div標籤和 相應的class=number屬性標籤。

  1. li_plants=soup.li  

 

  1. li_plants.clear()  


輸出:

<li class="producerlist"></li>

能夠看出跟li相關的標籤內容被刪除乾淨。


修改內容的特別函數

除了咱們以前看到的那些方法,BeautifulSoup還有其餘修改內容的方法。

  • Insert_after()和Insert_before()方法:

這兩個方法用於在標籤或字符串以前或以後插入標籤或字符串。這個方法須要的參數只有NavigavleString和tag對象。

  1. soup = BeautifulSoup(html_markup,"lxml")  
  2. div_number = soup.find("div",class_="number")  
  3. div_ecosystem = soup.new_tag("div")  
  4. div_ecosystem['class'] = "ecosystem"  
  5. div_ecosystem.append("soil")  
  6. div_number.insert_after(div_ecosystem)  
  7. print(soup.prettify())  


輸出:

<html>
<body>
<div class="ecopyramid">
<ul id="producers">
<li class="producerlist">
<div class="name">
plants
</div>
<div class="number">
100000
</div>
<div class="ecosystem">
soil
</div>

</li>
<li class="producerlist">
<div class="name">
algae
</div>

<div class="number">
100000
</div>
</li>
</ul>
</div>
</body>
</html>


  • replace_with()方法:

這個方法用於用一個新的標籤或字符串替代原有的標籤或字符串。這個方法把一個標籤對象或字符串對象做爲輸入。replace_with()會返回一個被替代標籤或字符串的句柄。

  1. soup = BeautifulSoup(html_markup,"lxml")  
  2. div_name =soup.div  
  3. div_name.string.replace_with("phytoplankton")  
  4. print(soup.prettify())  

 

replace_with()一樣也能夠用於徹底的替換掉一個標籤。

  • wrap()和unwrap()方法:

wrap()方法用於在一個標籤或字符串外包裹一個標籤或字符串。好比能夠用一個div標籤包裹li標籤裏的所有內容。

  1. li_tags = soup.find_all("li")  
  2. for li in li_tags:  
  3. <span style="white-space:pre">    </span>new_divtag = soup.new_tag("div")  
  4. <span style="white-space:pre">    </span>li.wrap(new_divtag)  
  5. print(soup.prettify())  


而unwrap()就跟wrap()作的事情相反。unwrap()和replace_with()同樣會返回被替代的標籤句柄。

相關文章
相關標籤/搜索