python爬虫:使用BeautifulSoup修改网页内容

BeautifulSoup除了可以查找和定位网页内容,还可以修改网页。修改意味着可以增加或删除标签,改变标签名字,变更标签属性,改变文本内容等等。

 使用修BeautifulSoup修改标签

每一个标签在BeautifulSoup里面都被当作一个标签对象,这个对象可以执行以下任务:

  • 修改标签名
  • 修改标签属性
  • 增加新标签
  • 删除存在的标签
  • 修改标签的文本内容

修改标签的名字

只需要修改.name参数就可以修改标签名字。

  1. producer_entries.name = "div"<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">怎么办嘛</span><img src="file:///C:UsersADMINI~1AppDataLocalTemp~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的字典。

更新已经存在属性的标签

可以参照如下代码:

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

为一个标签增加一个新的属性

比如一个标签没有class属性,那么可以参照如下代码增加class属性,

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

删除标签属性

使用del操作符,示例如下:

  1. del producer_entries['class']  

增加一个新的标签

BeautifulSoup有new_tag()方法来创造一个新的标签。然后可以使用append(),insert(),insert_after()或者insert_before()等方法来对新标签进行插入。

增加一个新生产者,使用new_tag()然后append()

参照前面例子,生产者除了plants和alage外,我们现在添加一个phytoplankton.首先,需要先创造一个li标签。

用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()一样会返回被替代的标签句柄。

原文地址:https://www.cnblogs.com/yizhenfeng168/p/6999355.html