Python BeautifulSoup基本使用
2016-09-13 08:55
531 查看
1. Beautiful Soup的安装:
可以利用 pip 或者 easy_install 来安装,以下两种方法均可easy_install beautifulsoup4
pip install beautifulsoup4
2. 创建BeautifulSoup对象
导入BeautifulSoup库from bs4 import BeautifulSoup
创建一个字符串方面用它来演示
html = """ <html><head><title>The Dormouse's story</title></head> <body> <p class="title" name="dromouse"><b>The Dormouse's story</b></p> <p class="story">Once upon a time there were three little sisters; and their names were <a href="http://example.com/elsie" class="sister" id="link1"><!-- Elsie --></a>, <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p> """
创建 beautifulsoup 对象
soup = BeautifulSoup(html)
另外,我们还可以用本地 HTML 文件来创建对象,例如
soup = BeautifulSoup(open(‘index.html’))
上面这句代码便是将本地 index.html 文件打开,用它来创建 soup 对象
下面我们来打印一下 soup 对象的内容,格式化输出
print soup.prettify()
<html> <head> <title> The Dormouse's story </title> </head> <body> <p class="title" name="dromouse"> <b> The Dormouse's story </b> </p> <p class="story"> Once upon a time there were three little sisters; and their names were <a class="sister" href="http://example.com/elsie" id="link1"> <!-- Elsie --> </a> , <a class="sister" href="http://example.com/lacie" id="link2"> Lacie </a> and <a class="sister" href="http://example.com/tillie" id="link3"> Tillie </a> ; and they lived at the bottom of a well. </p> <p class="story"> ... </p> </body> </html>
以上便是输出结果,格式化打印出了它的内容,这个函数经常用到,小伙伴们要记好咯。
5. 四大对象种类
Beautiful Soup将复杂HTML文档转换成一个复杂的树形结构,每个节点都是Python对象,所有对象可以归纳为4种:- Tage
- NavigableString
- BeautifulSoup
- Comment
1. Tag( HTML 中的一个个标签)
下面每一段代码中注释部分即为运行结果print(soup.title) #<title>The Dormouse's story</title>
print(soup.head) #<head><title>The Dormouse's story</title></head>
我们可以利用 soup加标签名轻松地获取这些标签的内容,是不是感觉比正则表达式方便多了?不过有一点是,它查找的是在所有内容中的第一个符合要求的标签,如果要查询所有的标签,我们在后面进行介绍。
我们可以验证一下这些对象的类型
print(type(soup.a))
#
name
print(soup.name) print(soup.head.name) #[document] #head
attrs
print(soup.p.attrs) #{'class': ['title'], 'name': 'dromouse'}
在这里,我们把 p 标签的所有属性打印输出了出来,得到的类型是一个字典。
如果我们想要单独获取某个属性,可以这样,例如我们获取它的 class 叫什么
print(soup.p['class']) #['title']
还可以这样,利用get方法,传入属性的名称,二者是等价的
print soup.p.get('class') #['title']
我们可以对这些属性和内容等等进行修改,例如
soup.p['class']="newClass" print(soup.p) #<p class="newClass" name="dromouse"><b>The Dormouse's story</b></p>
还可以对这个属性进行删除,例如
del soup.p['class'] print soup.p #<p name="dromouse"><b>The Dormouse's story</b></p>
不过,对于修改删除的操作,不是我们的主要用途,在此不做详细介绍了,如果有需要,请查看前面提供的官方文档
2. NavigableString
既然我们已经得到了标签的内容,那么问题来了,我们要想获取标签内部的文字怎么办呢?很简单,用 .string 即可,例如print soup.p.string #The Dormouse's story
这样我们就轻松获取到了标签里面的内容,想想如果用正则表达式要多麻烦。它的类型是一个 NavigableString,翻译过来叫 可以遍历的字符串,不过我们最好还是称它英文名字吧。
来检查一下它的类型
print type(soup.p.string) #<class 'bs4.element.NavigableString'>
3. BeautifulSoup
BeautifulSoup 对象表示的是一个文档的全部内容.大部分时候,可以把它当作 Tag 对象,是一个特殊的 Tag,我们可以分别获取它的类型,名称,以及属性来感受一下print(type(soup.name)) #<type 'unicode'> print(soup.name) # [document] print(soup.attrs) #{} 空字典
4. Comment
Comment 对象是一个特殊类型的 NavigableString 对象,其实输出的内容仍然不包括注释符号,但是如果不好好处理它,可能会对我们的文本处理造成意想不到的麻烦。我们找一个带注释的标签
print soup.a print soup.a.string print type(soup.a.string)
运行结果如下
<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a> Elsie <class 'bs4.element.Comment'>
a 标签里的内容实际上是注释,但是如果我们利用 .string 来输出它的内容,我们发现它已经把注释符号去掉了,所以这可能会给我们带来不必要的麻烦。
另外我们打印输出下它的类型,发现它是一个 Comment 类型,所以,我们在使用前最好做一下判断,判断代码如下
if type(soup.a.string)==bs4.element.Comment: print(soup.a.string)
上面的代码中,我们首先判断了它的类型,是否为 Comment 类型,然后再进行其他操作,如打印输出。
6. 遍历文档树
7. 搜索文档树
8. CSS选择器
部分转自:http://cuiqingcai.compy相关文章推荐
- [Python] - 爬虫之Beautiful Soup的基本使用
- python—beautifulsoup介绍与安装使用
- windows和linux下使用python2.7 urllib.urlopen+beautifulsoup打开12306网站订票页面表现不同,前者报错ssl认证失败,后者成功
- Windows下使用BeautifulSoup 出现You are trying to run the Python 2 version of Beautiful Soup under Python
- 使用python抓取并分析数据—链家网(requests+BeautifulSoup)(转)
- 萌新的Python学习日记 - 爬虫无影 - 使用BeautifulSoup + urlretrieve 抓取并保存图片:weheartit
- 使用requests+beautifulsoup模块实现python网络爬虫功能
- Python爬取淘宝搜索页,使用Selenium+BeautifulSoup
- 萌新的Python学习日记 - 爬虫无影 - 使用BeautifulSoup + css selector 抓取自己想要网页内容
- 萌新的Python学习日记 - 爬虫无影 - 使用BeautifulSoup + css selector 抓取动态网页内容:Knewone
- Python使用Selenium+BeautifulSoup爬取淘宝搜索页
- Python 3.0安装BeautifulSoup插件并使用 与Python 2 version of Beautiful Soup under Python 3问题处理
- Python BeautifulSoup的使用
- 使用Python+selenium+BeautifulSoup抓取动态网页的关键信息
- Python beautifulsoup的安装与使用
- python3 requests+BeautifulSoup使用多进程爬取妹子图
- python类使用基本方法
- python数据持久存储:pickle模块的基本使用
- python数据持久存储:pickle模块的基本使用 分类: python python基础学习 python 小练习 2013-06-17 14:41 209人阅读 评论(0) 收藏
- Python-memcached安装&基本使用