爬虫系列12.BS4修改文档树
2017-11-13 11:33
183 查看
修改tag的名称和属性
重命名一个tag,改变属性的值,添加或删除属性:
soup = BeautifulSoup(‘Extremely bold[/b]‘)
tag = soup.b
修改 .string
给tag的 .string 属性赋值,就相当于用当前的内容替代了原来的内容:
注意: 如果当前的tag包含了其它tag,那么给它的 .string 属性赋值会覆盖掉原有的所有内容包括子tag
append()
Tag.append() 方法想tag中添加内容,就好像Python的列表的 .append() 方法
NavigableString() 和 .new_tag()
如果想添加一段文本内容到文档中也没问题,可以调用Python的 append() 方法 或调用 NavigableString 的构造方法:
soup = BeautifulSoup(““)
tag = soup.b
tag.append(“Hello”)
new_string = NavigableString(” there”)
tag.append(new_string)
tag
# Hello there.
tag.contents
# [u’Hello’, u’ there’]
如果想要创建一段注释,或 NavigableString 的任何子类, 只要调用 NavigableString 的构造方法:
from bs4 import Comment
new_comment = soup.new_string(“Nice to see you.”, Comment)
tag.append(new_comment)
tag
# Hello there
tag.contents
# [u’Hello’, u’ there’, u’Nice to see you.’]
insert()
Tag.insert() 方法与 Tag.append() 方法类似,区别是不会把新元素添加到父节点.contents 属性的最后,而是把元素插入到指定的位置.与Python列表总的 .insert() 方法的用法下同:
markup = ‘I linked to example.com’
soup = BeautifulSoup(markup)
tag = soup.a
insert_before() 和 insert_after()
insert_before() 方法在当前tag或文本节点前插入内容:
insert_after() 方法在当前tag或文本节点后插入内容:
soup = BeautifulSoup(“stop“)
tag = soup.new_tag(“i”)
tag.string = “Don’t”
soup.b.string.insert_before(tag)
soup.b
# Don’tstop
clear()
Tag.clear() 方法移除当前tag的内容
extract()
PageElement.ex
4000
tract() 方法将当前tag移除文档树,并作为方法结果返回。
decompose()
Tag.decompose() 方法将当前节点移除文档树并完全销毁
replace_with()
PageElement.replace_with() 方法移除文档树中的某段内容,并用新tag或文本节点替代它
wrap()
PageElement.wrap() 方法可以对指定的tag元素进行包装 [8] ,并返回包装后的结果:
soup = BeautifulSoup(“
I wish I was bold.
“)
soup.p.string.wrap(soup.new_tag(“b”))
# I wish I was bold.
unwrap()
Tag.unwrap() 方法与 wrap() 方法相反.将移除tag内的所有tag标签,该方法常被用来进行标记的解包:
与 replace_with() 方法相同, unwrap() 方法返回被移除的tag
markup = ‘I linked to example.com’
soup = BeautifulSoup(markup)
a_tag = soup.a
重命名一个tag,改变属性的值,添加或删除属性:
soup = BeautifulSoup(‘Extremely bold[/b]‘)
tag = soup.b
tag.name = "blockquote" tag['class'] = 'verybold' tag['id'] = 1 tag # <blockquote class="verybold" id="1">Extremely bold</blockquote> del tag['class'] del tag['id'] tag # <blockquote>Extremely bold</blockquote>
修改 .string
给tag的 .string 属性赋值,就相当于用当前的内容替代了原来的内容:
注意: 如果当前的tag包含了其它tag,那么给它的 .string 属性赋值会覆盖掉原有的所有内容包括子tag
append()
Tag.append() 方法想tag中添加内容,就好像Python的列表的 .append() 方法
NavigableString() 和 .new_tag()
如果想添加一段文本内容到文档中也没问题,可以调用Python的 append() 方法 或调用 NavigableString 的构造方法:
soup = BeautifulSoup(““)
tag = soup.b
tag.append(“Hello”)
new_string = NavigableString(” there”)
tag.append(new_string)
tag
# Hello there.
tag.contents
# [u’Hello’, u’ there’]
如果想要创建一段注释,或 NavigableString 的任何子类, 只要调用 NavigableString 的构造方法:
from bs4 import Comment
new_comment = soup.new_string(“Nice to see you.”, Comment)
tag.append(new_comment)
tag
# Hello there
tag.contents
# [u’Hello’, u’ there’, u’Nice to see you.’]
这是Beautiful Soup 4.2.1 中新增的方法,创建一个tag最好的方法是调用工厂方法 BeautifulSoup.new_tag(),第一个参数作为tag的name,是必填,其它参数选填 :
soup = BeautifulSoup("<b></b>") original_tag = soup.b new_tag = soup.new_tag("a", href="http://www.example.com") original_tag.append(new_tag) original_tag # <b><a href="http://www.example.com"></a></b> new_tag.string = "Link text." original_tag # <b><a href="http://www.example.com">Link text.</a></b>
insert()
Tag.insert() 方法与 Tag.append() 方法类似,区别是不会把新元素添加到父节点.contents 属性的最后,而是把元素插入到指定的位置.与Python列表总的 .insert() 方法的用法下同:
markup = ‘I linked to example.com’
soup = BeautifulSoup(markup)
tag = soup.a
tag.insert(1, "but did not endorse ") tag # <a href="http://example.com/">I linked to but did not endorse <i>example.com</i></a>. # [u'I linked to ', u'but did not endorse', <i>example.com</i>]
insert_before() 和 insert_after()
insert_before() 方法在当前tag或文本节点前插入内容:
insert_after() 方法在当前tag或文本节点后插入内容:
soup = BeautifulSoup(“stop“)
tag = soup.new_tag(“i”)
tag.string = “Don’t”
soup.b.string.insert_before(tag)
soup.b
# Don’tstop
clear()
Tag.clear() 方法移除当前tag的内容
extract()
PageElement.ex
4000
tract() 方法将当前tag移除文档树,并作为方法结果返回。
decompose()
Tag.decompose() 方法将当前节点移除文档树并完全销毁
replace_with()
PageElement.replace_with() 方法移除文档树中的某段内容,并用新tag或文本节点替代它
wrap()
PageElement.wrap() 方法可以对指定的tag元素进行包装 [8] ,并返回包装后的结果:
soup = BeautifulSoup(“
I wish I was bold.
“)
soup.p.string.wrap(soup.new_tag(“b”))
# I wish I was bold.
soup.p.wrap(soup.new_tag("div")) # <div><p><b>I wish I was bold.</b></p></div>
unwrap()
Tag.unwrap() 方法与 wrap() 方法相反.将移除tag内的所有tag标签,该方法常被用来进行标记的解包:
与 replace_with() 方法相同, unwrap() 方法返回被移除的tag
markup = ‘I linked to example.com’
soup = BeautifulSoup(markup)
a_tag = soup.a
a_tag.i.unwrap() a_tag # <a href="http://example.com/">I linked to example.com</a>
相关文章推荐
- ElasticSearch笔记系列(6)——文档之修改的两种方式
- MongoDB学习系列2:shell中如何查询文档,修改文档,删除文档
- Python爬虫系列(六):搜索文档树
- Python爬虫系列之四:利用Python爬取PyODPS页面并整合成PDF文档
- python机器学习案例系列教程——文档分类器,朴素贝叶斯分类器,费舍尔分类器
- 《Entity Framework 6 Recipes》中文翻译系列 (29) ------ 第五章 加载实体和导航属性之过滤预先加载的实体集合和修改外键关联
- 对Document library中修改文档的Author(改进)
- 详解Node.js API系列 Http模块(2) CNodejs爬虫实现
- Android高效率编码-第三方SDK详解系列(二)——Bmob后端云开发,实现登录注册,更改资料,修改密码,邮箱验证,上传,下载,推送消息,缩略图加载等功能
- 水晶报表打印时如何修改打印文档名
- mybatis-generator扩展教程系列 -- 自定义配置参数修改DAO,Mapper文件后缀
- JAVA帮助文档全系列 JDK1.5 JDK1.6 JDK1.7 官方中英完整版下载
- 【爬虫-反爬虫】系列一:基础之模拟请求(2)
- Aurora转换现存的Tex文档 修改多个公式 在word中设置 Aurora快捷键
- 需求文档的类别及适用情况(JBean译文之Michael产品管理系列-1)
- android系列:第三篇android调试常用工具:模拟按键输入,修改分辨率,获得按键消息
- [导入]需求文档的类别及适用情况(JBean译文之Michael产品管理系列-1)
- TC官方文档翻译08----内存树数据库API(Tokyo Cabinet/Tokyo Tyarnt 文档系列)
- python爬虫系列文章
- 一个简易的网页爬虫,可用于下载在线API文档