Python利用Beautiful Soup模块修改内容方法示例
前言
其实BeautifulSoup模块除了能够搜索和导航之外,还能够修改HTML/XML文档的内容。这就意味着能够添加或删除标签、修改标签名称、改变标签属性值和修改文本内容等等。这篇文章非常详细的给大家介绍了Python利用BeautifulSoup模块修改内容的方法,下面话不多说,来看看详细的介绍吧。
修改标签
使用的示例HTML文档还是如下:
html_markup="""plants
前言
其实BeautifulSoup模块除了能够搜索和导航之外,还能够修改HTML/XML文档的内容。这就意味着能够添加或删除标签、修改标签名称、改变标签属性值和修改文本内容等等。这篇文章非常详细的给大家介绍了Python利用BeautifulSoup模块修改内容的方法,下面话不多说,来看看详细的介绍吧。
修改标签
使用的示例HTML文档还是如下:
html_markup="""plants
修改标签名称
soup=BeautifulSoup(html_markup,'lxml') producer_entries=soup.ul printproducer_entries.name producer_entries.name="div" printproducer_entries.prettify()
修改标签属性值
#修改标签属性 #更新标签现有的属性值 producer_entries['id']="producers_new_value" printproducer_entries.prettify() #标签添加新的属性值 producer_entries['class']="newclass" printproducer_entries.prettify() #删除标签属性值 delproducer_entries['class'] printproducer_entries.prettify()
添加新的标签
我们可以使用new_tag方法来生成一个新的标签,然后使用append()、insert()、insert_after()、insert_before()方法来将标签添加到HTML树中。
例如在上述的HTML文档的ul标签中添加一个li标签。首先要生成新的li标签,然后将其插入到HTML树结构中。并在li标签中插入相应的div标签。
#添加新的标签 #new_tag生成一个tag对象 new_li_tag=soup.new_tag("li") #标签对象添加属性的方法 new_atag=soup.new_tag("a",href="www.example.com"rel="externalnofollow") new_li_tag.attrs={'class':'producerlist'} soup=BeautifulSoup(html_markup,'lxml') producer_entries=soup.ul #使用append()方法添加到末尾 producer_entries.append(new_li_tag) printproducer_entries.prettify() #生成两个div标签,将其插入到li标签中 new_div_name_tag=soup.new_tag("div") new_div_name_tag['class']="name" new_div_number_tag=soup.new_tag("div") new_div_number_tag["class"]="number" #使用insert()方法指定位置插入 new_li_tag.insert(0,new_div_name_tag) new_li_tag.insert(1,new_div_number_tag) printnew_li_tag.prettify()
修改字符串内容
修改字符串内容可以使用new_string() 、append()、insert()方法。
#修改字符串内容 #使用.string属性修改字符串内容 new_div_name_tag.string='new_div_name' #使用.append()方法添加字符串内容 new_div_name_tag.append("producer") #使用soup对象的new_string()方法生成字符串 new_string_toappend=soup.new_string("producer") new_div_name_tag.append(new_string_toappend) #使用insert()方法插入 new_string_toinsert=soup.new_string("10000") new_div_number_tag.insert(0,new_string_toinsert) printproducer_entries.prettify()
删除标签节点
BeautifulSoup模块提供了decompose()和extract()方法来删除节点。
decompose()方法删除节点,不仅会删除当前节点,还会把其子节点一块删除了。
extract()方法用来从HTML树中删除节点或者字符串内容。
#删除节点 third_producer=soup.find_all("li")[2] #使用decompose()方法删除div节点 div_name=third_producer.div div_name.decompose() printthird_producer.prettify() #使用extract()方法删除节点 third_producer_removed=third_producer.extract() printsoup.prettify()
删除标签内容
标签可能有NavigableString对象或者Tag对象作为它的子节点,移除所有的这些子节点可以使用clear()方法。这将会移除标签的所有的.content。
修改内容的其他方法
除了上面说到的方法,还有其他方法用来修改内容。
insert_after()和insert_before()方法
上面的两个方法能够在标签或者字符串的前面或者后面插入一个标签或者字符串。方法只能接收一个参数,要么是NavigableString对象要么是Tag对象。
replace_with()方法
该方法是用一个新的标签或字符串内容替代原来的标签或者字符串,能够接收一个标签或者字符串作为输入。
wrap()和unwrap()方法
wrap()方法是用另一个标签来包裹一个标签或者字符串。
unwrap()方法则和wrap()方法相反。
#wrap()方法 li_tags=soup.find_all('li') forliinli_tags: new_div_tag=soup.new_tag('div') li.wrap(new_div_tag) printsoup.prettify() #unwrap()方法 li_tags=soup.find_all("li") forliinli_tags: li.div.unwrap() printsoup.prettify()
总结
以上就是关于Python使用BeautifulSoup模块修改内容的全部内容了,希望本文的内容对大家学习或者使用python能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对毛票票的支持。