python 爬虫利器 Beautiful Soup

Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你节省数小时甚至数天的工作时间.

这里不再介绍其安装过程,可以同anaconda 管理工具一步安装,并自动安装依赖的相关包。

Beautiful Soup 使用

# 首先从 bs4 导入
from bs4 inport BeautifulSoup

简单实用举例说明

from bs4 import BeautifulSoup

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>
'''

使用BeautifulSoup解析这段代码,能够得到一个 BeautifulSoup 的对象,并能按照标准的缩进格式的结构输出:

soup = BeautifulSoup(html,'lxml')
print('***'*10)
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>

其他属性输出

print(soup.title)
# <title>The Dormouse's story</title>
print(soup.title.name)
# title
print(soup.title.string)
# The Dormouse's story
print(soup.title.parent.name)
# head
print(soup.p)
# <p class="title"><b>The Dormouse's story</b></p>
print(soup.p["class"])
# ['title']
print(soup.a)
# <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
print(soup.find_all('a'))
# [<a class="sister" href="http://example.com/elsie" 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>]
print(soup.find(id='link3'))
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>
# 从文档中找到所有<a>标签的链接:
# for link in soup.findAll('a'):
# print(link.get('href'))
for link in soup.find_all('a'):
print(link.get('href')) # http://example.com/elsie
# http://example.com/lacie
# http://example.com/tillie # 从文档中获取所有文字内容:
# print(soup.getText())
print(soup.get_text()) # The Dormouse's story
#
# 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.
# ...

Beautiful Soup解析器

Beautiful Soup支持Python标准库中的HTML解析器,还支持一些第三方的解析器,其中一个是 lxml

下表列出了主要的解析器,以及它们的优缺点:

markup = "<b><!--Hey, buddy. Want to buy a used parser?--></b>"
解析器 使用方法 优势 劣势
Python标准库 BeautifulSoup(markup, “html.parser”)
  • Python的内置标准库
  • 执行速度适中
  • 文档容错能力强
  • Python 2.7.3 or 3.2.2)前 的版本中文档容错能力差
lxml HTML 解析器 BeautifulSoup(markup, “lxml”)
  • 速度快
  • 文档容错能力强
  • 需要安装C语言库
lxml XML 解析器 BeautifulSoup(markup, [“lxml”, “xml”])BeautifulSoup(markup, “xml”)
  • 速度快
  • 唯一支持XML的解析器
  • 需要安装C语言库
html5lib BeautifulSoup(markup, “html5lib”)
  • 最好的容错性
  • 以浏览器的方式解析文档
  • 生成HTML5格式的文档
  • 速度慢
  • 不依赖外部扩展

对象的种类

Beautiful Soup将复杂HTML文档转换成一个复杂的树形结构,每个节点都是Python对象,所有对象可以归纳为4种: Tag , NavigableString , BeautifulSoup , Comment .

【Tag】

tag 对象类似于一个标签节点。与XML或HTML原生文档中的标签相同,如 body,div,a,span。tag 对象有很多方法和属性。tag 对象的属性可以像字典一样进行增删改查操作。

 name 属性

name 属性表示 tag 的名称。通过 .name 获取。如果改变了tag的name,那将影响所有通过当前Beautiful Soup对象生成的HTML文档。

# 获取 tag 名字为 a 的标签

tag = soup.a
print(tag)
# <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a> print(tag.name)
# a
print(soup.name)
# [document]

attributes 属性

一个tag可能有很多个属性,使用 tag.attrs 获取 tag 的所有节点属性,可以对这些属性进行增删改查。获取方法如下:

  • tag.attrs:获取属性列表
  • tag.attrs[1]:获取属性列表中的第2个属性
  • tag.get('href'):获取 href 属性
  • tag['href']:获取 href 属性
# 获取 tag 名字为 a 的标签

tag = soup.a
print(tag)
# <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a> print(tag.name)
# a print(tag.attrs)
# {'href': 'http://example.com/elsie', 'class': ['sister'], 'id': 'link1'} print(tag.get('href'))
# http://example.com/elsie print(tag.get('class'))
# ['sister']
print(tag['class'])
# ['sister']
print(tag['id'])
# link1

多值属性

在HTML文档中有典型像 class 一样的有多个属性值,这些多值属性返回的值不是 string ,而是 list 。这些多值属性的节点类型如下:

  • class
  • rel
  • rev
  • accept-charset
  • headers
  • accesskey

在XML文档中没有多值属性

print(tag['class'])
# ['sister']

tag的属性可以被添加,删除或修改. 再说一次, tag的属性操作方法与字典一样

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> tag['class']
# KeyError: 'class'
print(tag.get('class'))
# None

【NavigableString】

字符串常被包含在tag内.Beautiful Soup用 NavigableString 类来包装tag中的字符串:

<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
soup = BeautifulSoup('<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>', 'lxml')

tag = soup.a

print(tag)
# <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
print(tag.string)
# Elsie

这样我们就轻松获取到了标签里面的内容,想想如果用正则表达式要多麻烦。它的类型是一个 NavigableString,翻译过来叫 可以遍历的字符串,不过我们最好还是称它英文名字吧。

tag中包含的字符串不能编辑,但是可以被替换成其它的字符串,用 replace_with() 方法:

tag.string.replace_with('Jack')
print(tag)
# <a class="sister" href="http://example.com/elsie" id="link1">Jack</a>
print(tag.string)
# Jack

【BeautifulSoup】

BeautifulSoup 对象表示的是一个文档的全部内容.大部分时候,可以把它当作 Tag 对象,它支持 遍历文档树 和 搜索文档树 中描述的大部分的方法.

因为 BeautifulSoup 对象并不是真正的HTML或XML的tag,所以它没有name和attribute属性.但有时查看它的 .name 属性是很方便的,所以 BeautifulSoup 对象包含了一个值为 “[document]” 的特殊属性 .name

soup = BeautifulSoup(html,'lxml')

# print(soup.prettify())
print(soup.name)
# [document]

【注释及特殊字符串-Comment】

Tag , NavigableString , BeautifulSoup 几乎覆盖了html和xml中的所有内容,但是还有一些特殊对象.容易让人担心的内容是文档的注释部分:

markup = "<b><!--Hey, buddy. Want to buy a used parser?--></b>"
soup = BeautifulSoup(markup)
comment = soup.b.string
print(comment)
# Hey, buddy. Want to buy a used parser?
print(type(comment))
# <class 'bs4.element.Comment'>

Comment 对象是一个特殊类型的 NavigableString 对象.

简单使用先介绍到这里,如需详细学习,可参考https://beautifulsoup.readthedocs.io/zh_CN/v4.4.0/#

python 爬虫利器 Beautiful Soup的更多相关文章

  1. Python爬虫之Beautiful Soup解析库的使用(五)

    Python爬虫之Beautiful Soup解析库的使用 Beautiful Soup-介绍 Python第三方库,用于从HTML或XML中提取数据官方:http://www.crummv.com/ ...

  2. [Python爬虫] 使用 Beautiful Soup 4 快速爬取所需的网页信息

    [Python爬虫] 使用 Beautiful Soup 4 快速爬取所需的网页信息 2018-07-21 23:53:02 larger5 阅读数 4123更多 分类专栏: 网络爬虫   版权声明: ...

  3. python爬虫之Beautiful Soup基础知识+实例

    python爬虫之Beautiful Soup基础知识 Beautiful Soup是一个可以从HTML或XML文件中提取数据的python库.它能通过你喜欢的转换器实现惯用的文档导航,查找,修改文档 ...

  4. python爬虫之Beautiful Soup的基本使用

    1.简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: Beautiful Soup提供一些简单的.python式的函数用来处理导航.搜索 ...

  5. Python爬虫库-Beautiful Soup的使用

    Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库,简单来说,它能将HTML的标签文件解析成树形结构,然后方便地获取到指定标签的对应属性. 如在上一篇文章通过爬虫 ...

  6. python 爬虫5 Beautiful Soup的用法

    1.创建 Beautiful Soup 对象 from bs4 import BeautifulSoup html = """ <html><head& ...

  7. Python爬虫之Beautiful Soup库的基本使用

  8. Python爬虫利器二之Beautiful Soup的用法

    上一节我们介绍了正则表达式,它的内容其实还是蛮多的,如果一个正则匹配稍有差池,那可能程序就处在永久的循环之中,而且有的小伙伴们也对写正则表达式的写法用得不熟练,没关系,我们还有一个更强大的工具,叫Be ...

  9. python爬虫利器Selenium使用详解

    简介: 用pyhon爬取动态页面时普通的urllib2无法实现,例如下面的京东首页,随着滚动条的下拉会加载新的内容,而urllib2就无法抓取这些内容,此时就需要今天的主角selenium. Sele ...

随机推荐

  1. 关于actor模型

    actor model是1973年就提出的一个分布式并发编程模型,在erlang语言中得到广泛支持和应用.目前Java中也出现了很多支持actor模型的库:akka.killim.jetlang等等, ...

  2. Springbooot +Mybaties 配置数据库多数据源

    前言 在实际项目中,我们可能会碰到在一个项目中会访问多个数据库的情况.针对这种情况,我们就需要配置动态的数据源了.一般按照以下步骤即可 一.在启动类上添加注解 二.在application.prope ...

  3. halcon+csharp多图像拼接实现

    简单的来说,就是将 一类的图片最后拼接成为这样的结果 这个图片有点大呀. 基本步骤: 1.halcon进行仿射变化进行镜头畸变.这个可以参考halcon中一个二维码畸变的例子: 2.基于模版匹配找出偏 ...

  4. JavaScript中this的用法 及 如何改变this的指向

    要懂得JavaScript中this的用法,首先需要知道,JavaScript中的作用域相关知识. var fun = function(){ var flag = 1; console.log(fl ...

  5. Codeforces 837E Vasya's Function - 数论

    Vasya is studying number theory. He has denoted a function f(a, b) such that: f(a, 0) = 0; f(a, b) = ...

  6. bzoj 2820 YY的GCD - 莫比乌斯反演 - 线性筛

    Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对kAc这种 傻×必 ...

  7. Python3 tkinter基础 Button command 单击按钮 在console中打印文本

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  8. Ubuntu: repository/PPA 源

    在Ubuntu中,每个PPA源是单独存放在/etc/apt/sources.list.d/文件夹中的,进入到该文件夹,使用ls命令查询即可列出当前系统添加的PPA源. 添加 sudo add-apt- ...

  9. What is the difference between visibility:hidden and display:none?

    What is the difference between visibility:hidden and display:none? 答案1 display:none means that the t ...

  10. (转)Autonomous_Vehicle_Paper_Reading_List

    Autonomous_Vehicle_Paper_Reading_List 2018-07-19 10:40:08 Reference:https://github.com/ZRZheng/Auton ...