BeautifulSoup使用总结
一、介绍
BeautifulSoup为一个python库,它可以接收一个HTML或XML的字符串或文件,并返回一个BeautifulSoup对象,之后我们可以使用BeautifulSoup提供的众多方法来对文件内容进行解析。
二、安装
1、使用pip安装
pip install beautifulsoup4
#安装BeautifulSoup解析器
pip install lxml
pip install html5lib
2、通过apt-get安装
sudo apt-get install Python-bs4
#安装BeautifulSoup解析器
sudo apt-get install Python-lxml
sudo apt-get install Python-html5lib
推荐使用lxml作为解析器,因为其效率更高。
三、常用方法
下面的例子将解析以下字符串:
html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><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>
"""
1、将字符串包装厂BeautifulSoup对象
soup = BeautifulSoup(html, "lxml")
#使用标准的缩进结构输出
print soup.prettify()
输出:
<html>
<head>
<title>
The Dormouse's story
</title>
</head>
<body>
<p class="title">
<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>
2、使用name获取标签名称
print soup.a
print soup.a.name
输出:
<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
a
需要注意的是,使用soup.[tag]来访问标签只会返回第一个名为tag的标签,若想返回所有的或者根据条件返回,可以使用find_all()方法。
3、使用string获取标签内容
通过访问标签的string属性可以获取标签的内容。
print soup.title.string
输出:
The Dormouse's story
需要注意的是使用string来访问标签内容时,该标签内只能包含一个子节点,若有多个子节点,使用string会返回None,因为不知道该返回哪个子节点的内容。
print soup.body.string
输出:
None
将string换成strings即可:
strings = soup.body.strings
for string in strings:
print string
输出:
The Dormouse's story
Once upon a time there were three little sisters; and their names were
Elsie
,
Lacie
and
Tillie
;
and they lived at the bottom of a well.
...
可以看到输出有很多多余的空行和空格,使用stripped_strings即可去除这些空行和空格:
strings = soup.body.stripped_strings
for string in strings:
print string
输出:
The Dormouse's story
Once upon a time there were three little sisters; and their names were
Elsie
,
Lacie
and
Tillie
;
and they lived at the bottom of a well.
...
4、获取标签的属性名称
#获取第一个<p>标签的class属性
soup.p["class"]
输出:
['title']
返回的为一个列表,因为class可能有多个值。
#获取第一个<a>标签的href属性
soup.a["href"]
输出:
'http://example.com/elsie'
5、更改标签的属性值
#更改第一个<p>标签的href属性
soup.p["class"] = "new-class"
print soup.p["class"]
#更改第一个<a>标签的href属性
soup.a["href"] = "www.google.com"
print soup.a["href"]
print soup.prettify()
输出:
new-class
www.google.com
<html>
<head>
<title>
The Dormouse's story
</title>
</head>
<body>
<p class="new-class">
<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="www.google.com" 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>
6、find_all方法
6.1 返回所有的标签
#返回文档中所有的<a>标签,返回值为列表
links = soup.find_all("a")
print links
输出:
[<a class="sister" href="www.google.com" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
6.2、根据属性名返回标签
#返回文档中所有的类名为sister的<a>标签,返回值为列表
#class为python关键字,所以使用class_代替
links = soup.find_all("a", class_="sister")
print links
print '-'*20
#与上面的相同
links = soup.find_all("a", attrs={"class":"sister"})
print links
print '-'*20
#返回文档中所有的id为link2的<a>标签,返回值为列表
links = soup.find_all("a", id="link2")
print links
输出:
[<a class="sister" href="www.google.com" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
--------------------
[<a class="sister" href="www.google.com" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
--------------------
[<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]
6.3、获取所有标签的href属性
links = soup.find_all("a")
for a in links:
print a["href"]
输出:
www.google.com
http://example.com/lacie
http://example.com/tillie
三、参考
1、https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html
BeautifulSoup使用总结的更多相关文章
- Python爬虫小白入门(三)BeautifulSoup库
# 一.前言 *** 上一篇演示了如何使用requests模块向网站发送http请求,获取到网页的HTML数据.这篇来演示如何使用BeautifulSoup模块来从HTML文本中提取我们想要的数据. ...
- 使用beautifulsoup与requests爬取数据
1.安装需要的库 bs4 beautifulSoup requests lxml如果使用mongodb存取数据,安装一下pymongo插件 2.常见问题 1> lxml安装问题 如果遇到lxm ...
- BeautifulSoup :功能使用
# -*- coding: utf-8 -*- ''' # Author : Solomon Xie # Usage : 测试BeautifulSoup一些用法及容易出bug的地方 # Envirom ...
- BeautifulSoup研究一
BeautifulSoup的文档见 https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/ 其中.contents 会将换行也记录为一个子节 ...
- BeautifulSoup
参考:http://www.freebuf.com/news/special/96763.html 相关资料:http://www.jb51.net/article/65287.htm 1.Pytho ...
- BeautifulSoup Some characters could not be decoded, and were replaced with REPLACEMENT CHARACTER.
BeautifulSoup很赞的东西 最近出现一个问题:Python 3.3 soup=BeautifulSoup(urllib.request.urlopen(url_path),"htm ...
- beautifulSoup(1)
import re from bs4 import BeautifulSoupdoc = ['<html><head><title>Page title</t ...
- python BeautifulSoup模块的简要介绍
常用介绍: pip install beautifulsoup4 # 安装模块 from bs4 import BeautifulSoup # 导入模块 soup = BeautifulSoup(ht ...
- BeautifulSoup 的用法
转自:http://cuiqingcai.com/1319.html Beautiful Soup支持Python标准库中的HTML解析器,还支持一些第三方的解析器,如果我们不安装它,则 Python ...
- BeautifulSoup的选择器
用BeautifulSoup查找指定标签(元素)的时候,有几种方法: soup=BeautifulSoup(html) 1.soup.find_all(tagName),返回一个指定Tag元素的列表 ...
随机推荐
- cf980E TheNumberGames (贪心+倍增)
由于是$2^i$,所以一定要尽量留下来编号大的点 我们干脆就让n号点做树根,它是一定要留的 然后如果要留i的话,i一直到根的路径也都要留.所以只要判断一下够不够把这个路径上还没有留的都留下来 记录下已 ...
- SpringBoot整合Mybatis之xml
SpringBoot整合Mybatis mybatis ORM框架.几个重要的概念: Mapper配置 : 可以使用基于XML的Mapper配置文件来实现,也可以使用基于Java注解的Mybatis注 ...
- Linux让git记住账号密码
Linux让git记住账号密码 ——IT唐伯虎 摘要: Linux让git记住账号密码. 1.进入根目录,指令:cd / 2.创建记录账号密码的文件,指令:touch .git-credentials ...
- SQL记录-PLSQL面向对象
PL/SQL面向对象 PL/SQL允许定义一个对象类型,这有助于在Oracle的数据库中设计的面向对象.对象类型可以包装复合类型.使用对象允许实现数据的具体结构现实世界中的对象和方法操作它.对象有属性 ...
- Python学习笔记5-时间模块time/datetime
import time time.sleep(2) #等待几秒 # 1.格式化好的时间 2018-1-14 16:42 # 2.时间戳 是从unix元年到现在所有的秒数 # 3.时间元组 #想时间戳和 ...
- JS 简易控制台插件 [供 博客, 论坛 运行js用]
今天厚着脸皮来推荐下鄙人写的一个小插件吧.看过我博客的应该都熟悉这个插件了,其实就是这货. 这东西是我去年写的,当时水平也不怎么样,不过好歹还是实现了简单功能.我先简单介绍下这东西什么用吧. 因为在 ...
- Tornado实现多线程、多进程HTTP服务
背景 线上有一个相关百科的服务,返回一个query中提及的百科词条.该服务是用python实现的,以前通过thrift接口访问,现要将其改为通过HTTP访问.之前没有搭建HTTPServer的经验,因 ...
- Zookeeper笔记之quota
一.节点配额概述 zookeeper中可以往节点存放数据,但是一般来说存放数据总是要有个度量的对吧,不然空间就那么大,如果某个节点将空间全占用了其它节点没得用了,所以zookeeper提供了一个对节点 ...
- Ubuntu GNOME单击任务栏图标最小化设置
在Ubuntu GNOME的发行版中,桌面使用的是GNOME,GNOME可以像Windows那样有一个底部任务栏,在Ubuntu GNOME中它称为 dash to dock,如下图: Windows ...
- C++(vs)多线程调试 (转)
在一个程序中,这些独立运行的程序片断叫作“线程”(Thread),利用它编程的概念就叫作“多线程处理”.利用线程,用户可按下一个按钮,然后程序会立即作出响应,而不是让用户等待程序完成了当前任务以后 ...