博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
BeautifulSoup
阅读量:7099 次
发布时间:2019-06-28

本文共 1997 字,大约阅读时间需要 6 分钟。

BeautifulSoup将复杂HTML文档转换成一个复杂的树形结构,每个节点都是Python对象,所有对象可以归纳为4种: Tag, NavigableString, BeautifulSoup, Comment. 1、Tag:
soup = BeautifulSoup('#The Dormouses story',"lxml")tag = soup.titleprint(tag)>>  The Dormouses story

Tag有两个重要的属性:name和attrs;

soup = BeautifulSoup('#The Dormouses story',"lxml")tag = soup.titleprint(tag.name)>> title
#利用name属性修改html文档 soup = BeautifulSoup('#The Dormouses story',"lxml") tag = soup.title tag.name = 'aaa' print(tag) >> 
The Dormouses story
#一个tag可能有很多个属性,如 tag  有一个 “class” 的属性,值为 “boldest” .soup = BeautifulSoup('Extremely bold',"lxml")tag = soup.bprint(tag.attrs)>>{
'class': ['boldest']}# tag的属性的操作方法与字典相同:print(tag['class'])>>['boldest']#tag的属性可以被修改#tag['class'] = 'verybold'#print(tag)>> Extremely bold#tag的属性可以被添加tag['grade'] = 'first'print(tag)>> Extremely bold#tag的属性可以被删除del tag['class']print(tag)>> Extremely bold

2、NavigableString:BeautifulSoup用NavigableString类来包装tag中的字符串

soup = BeautifulSoup('Extremely bold',"lxml")tag = soup.bprint(tag.string)>> Extremely bold
#tag中包含的字符串不能编辑,但是可以被替换成其它的字符串,用 replace_with() 方法:
tag.string.replace_with('change string') print(tag) >> change string

3、BeautifulSoup:表示的是一个文档的全部内容。大部分时候,可以把它当作Tag对象。

soup = BeautifulSoup('Extremely bold',"lxml")print(soup.attrs)>> {}print(soup.name)>> [document]

4、Comment:是一个特殊类型的NavigableString对象,为文档的注释部分。

soup = BeautifulSoup('
','lxml')tag = soup.bcomment = tag.stringprint(comment)>> Hey, buddy. Want to buy a used parser?

遍历文档树:

(1) Tag的名字:

html_doc = """The Dormouse's story    

The Dormouse's story

Once upon a time there were three little sisters; and their names wereElsie,Lacie andTillie;and they lived at the bottom of a well.

...

"""soup = BeautifulSoup(html_doc, 'html.parser')print(soup.head) #指定获取的tag的nameprint(soup.title)print(soup.a) # 当有多个该名称的tag时,只能获取到第一个print(soup.find_all('a')) # 查找全部名称为a的Tag

(2).contents 和 .children

tag的 .contents 属性可以将tag的子节点以列表的方式输出

(3).descendants:遍历子孙节点

 

 

转载于:https://www.cnblogs.com/weimusan/p/9278822.html

你可能感兴趣的文章
热词分享
查看>>
phpcms相关
查看>>
thinkphp空控制器的处理
查看>>
Unity优化----drawcall系列
查看>>
[转]Windows Server 2012体验之部署第一台域控制器
查看>>
ubuntu 安装 php curl
查看>>
软件包管理 之 关于Fedora Core 5.0 通过Yum在线升级说明
查看>>
Log4j使用指南
查看>>
Overview of package util.concurrent Release 1.3.4.
查看>>
C# 反射(Reflection)技术
查看>>
C# 字符串操作基本过程(Equals、Compare、EndsWith等处理方法)
查看>>
黑马程序员-关于C语言的指针一些小知识点
查看>>
实现类似街旁网的分享足迹功能
查看>>
Android中获取系统内存信息以及进程信息-----ActivityManager的使用(一)
查看>>
版本升级demo(thread+service+Notification)
查看>>
zookeeper
查看>>
解决AD9中“......has no driver”的问题
查看>>
通过按键实现LED灯的亮灭(含两种情况)
查看>>
Hibernate入门_增删改查
查看>>
C#中常用接口介绍
查看>>