• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python利用Beautiful Soup模块修改内容方法示例

Python利用Beautiful Soup模块修改内容方法示例

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python beautifulsoup,beautifulsoup,beautifulsoup安装,beautifulsoup官网,beautifulsoup下载等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

前言

其实Beautiful Soup 模块除了能够搜索和导航之外,还能够修改 HTML/XML 文档的内容。这就意味着能够添加或删除标签、修改标签名称、改变标签属性值和修改文本内容等等。这篇文章非常详细的给大家介绍了Python利用Beautiful Soup模块修改内容的方法,下面话不多说,来看看详细的介绍吧。

修改标签

使用的示例 HTML 文档还是如下:

html_markup="""
 <div class="ecopyramid">
 <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>
 </ul>
 </div>
 """
</div>

修改标签名称

soup = BeautifulSoup(html_markup,'lxml')
producer_entries = soup.ul
print producer_entries.name
producer_entries.name = "div"
print producer_entries.prettify()
</div>

修改标签属性值

# 修改标签属性
# 更新标签现有的属性值
producer_entries['id'] = "producers_new_value"
print producer_entries.prettify()
# 标签添加新的属性值
producer_entries['class'] = "newclass"
print producer_entries.prettify()
# 删除标签属性值
del producer_entries['class']
print producer_entries.prettify()
</div>

添加新的标签

我们可以使用 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="external nofollow" )
new_li_tag.attrs = {'class':'producerlist'}
soup = BeautifulSoup(html_markup,'lxml')
producer_entries = soup.ul
# 使用 append() 方法添加到末尾
producer_entries.append(new_li_tag)
print producer_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)
print new_li_tag.prettify()
</div>

修改字符串内容

修改字符串内容可以使用 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)
print producer_entries.prettify()
</div>

删除标签节点

Beautiful Soup 模块提供了 decompose() 和 extract() 方法来删除节点。

decompose() 方法删除节点,不仅会删除当前节点,还会把其子节点一块删除了。

extract() 方法用来从 HTML 树中删除节点或者字符串内容。

# 删除节点
third_producer = soup.find_all("li")[2]
# 使用 decompose() 方法删除 div 节点
div_name = third_producer.div
div_name.decompose()
print third_producer.prettify()
# 使用 extract() 方法删除节点
third_producer_removed = third_producer.extract()
print soup.prettify()
</div>

删除标签内容

标签可能有 NavigableString 对象或者 Tag 对象作为它的子节点,移除所有的这些子节点可以使用 clear() 方法。这将会移除标签的所有的 .content。

修改内容的其他方法

除了上面说到的方法,还有其他方法用来修改内容。

insert_after() 和 insert_before() 方法

上面的两个方法能够在标签或者字符串的前面或者后面插入一个标签或者字符串。方法只能接收一个参数,要么是 NavigableString 对象要么是 Tag 对象。

replace_with() 方法

该方法是用一个新的标签或字符串内容替代原来的标签或者字符串,能够接收一个标签或者字符串作为输入。

wrap() 和 unwrap() 方法

wrap() 方法是用另一个标签来包裹一个标签或者字符串。

unwrap() 方法则和 wrap() 方法相反。

# wrap()方法
li_tags = soup.find_all('li')
for li in li_tags:
 new_div_tag = soup.new_tag('div')
 li.wrap(new_div_tag)
print soup.prettify()
# unwrap()方法
li_tags = soup.find_all("li")
for li in li_tags:
 li.div.unwrap()
print soup.prettify()
</div>

总结

以上就是关于Python使用Beautiful Soup 模块修改内容的全部内容了,希望本文的内容对大家学习或者使用python能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。

</div>

您可能想查找下面的文章:

  • Python利用Beautiful Soup模块创建对象详解
  • Python利用Beautiful Soup模块修改内容方法示例
  • Python利用Beautiful Soup模块搜索内容详解
  • Python利用Beautiful Soup模块创建对象详解
  • Python利用Beautiful Soup模块修改内容方法示例
  • Python使用BeautifulSoup库解析HTML基本使用教程
  • python基于BeautifulSoup实现抓取网页指定内容的方法
  • Python中使用Beautiful Soup库的超详细教程
  • Python中使用Beautiful Soup库的超详细教程
  • python使用BeautifulSoup分页网页中超链接的方法

相关文章

  • Python3.x版本中新的字符串格式化方法
  • 如何将python中的List转化成dictionary
  • 一个计算身份证号码校验位的Python小程序
  • python 从远程服务器下载日志文件的程序
  • python模块之StringIO使用示例
  • Django中的CACHE_BACKEND参数和站点级Cache设置
  • python用于url解码和中文解析的小脚本(python url decoder)
  • python 获取网页编码方式实现代码
  • Python实现批量把SVG格式转成png、pdf格式的代码分享
  • python实现的登录和操作开心网脚本分享

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 简单的python后台管理程序
    • Python 基础教程之包和类的用法
    • python中enumerate的用法实例解析
    • Python切片用法实例教程
    • Python中字符编码简介、方法及使用建议
    • python 实现堆排序算法代码
    • RC4文件加密的python实现方法
    • python制作爬虫爬取京东商品评论教程
    • 使用PYTHON接收多播数据的代码
    • python求crc32值的方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有