解析库

解析器 使用方法 优势 劣势
Python标准库 BeautifulSoup(html, 'html.parser') 速度适中,容错能力强 老版本python容错能力差
lxml HTML解析库 BeautifulSoup(html, 'lxml') 速度快,容错能力强 安装c语言库
lxml XML解析库 BeautifulSoup(html, 'xml') 速度快,唯一支持XML的解析器 安装c语言库
html5lib BeautifulSoup(html, 'html5lib') 最高的容错性,浏览器方式解析文档,生成HTML格式文档 速度慢

基本使用

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

选择标签

soup = BeautifulSoup(html, 'lxml')
print(soup.title) # <title>The Dormouse's story</title>
print(type(soup.title)) # <class 'bs4.element.Tag'>
print(soup.head) # <head><title>The Dormouse's story</title></head>
print(soup.p) # <p class="title"><b>The Dormouse's story</b></p>

可以看到,当有多个标签满足条件时,只返回了第一个。

获取标签名称(.name)

soup = BeautifulSoup(html, 'lxml')
print(soup.title.name) # title

获取属性(.attrs)

HTML 4定义了一系列可以包含多个值的属性.在HTML5中移除了一些,却增加更多.最常见的多值的属性是 class (一个tag可以有多个CSS的class). 还有一些属性 rel , rev , accept-charset , headers , accesskey . 在Beautiful Soup中多值属性的返回类型是list:

如果某个属性看起来好像有多个值,但在任何版本的HTML定义中都没有被定义为多值属性,那么Beautiful Soup会将这个属性作为字符串返回

如果转换的文档是XML格式,那么tag中不包含多值属性

soup = BeautifulSoup(html, 'lxml')
print(soup.p.attrs['class']) # ['title']
print(soup.p['class']) # ['title']
print(soup.a.attrs['id']) # link1

以上两种方法都是可以实现的。

获取内容(.string/.strings)

soup = BeautifulSoup(html, 'lxml')
print(soup.p.string) # The Dormouse's story

嵌套选择

soup = BeautifulSoup(html, 'lxml')

# 获取head标签里面的title标签的内容
print(soup.head.title.string) # The Dormouse's story

子节点和子孙节点

contents以一个列表的形式返回所有的子节点

soup = BeautifulSoup(html, 'lxml')
# 将p标签下面的所有的子节点以列表的形式返回
print(soup.p.contents) # [<b>The Dormouse's story</b>]

children以一个迭代器的形式返回所有的子节点

soup = BeautifulSoup(html, 'lxml')
# 返回的是一个列表迭代器
print(soup.p.children) # <list_iterator object at 0x000002174E8C8128>
for i,child in enumerate(soup.p.children):
print(i,child) # 0 <b>The Dormouse's story</b>

descendants以一个迭代器的形式返回所有的子孙节点

soup = BeautifulSoup(html, 'lxml')
print(soup.p.descendants) # <generator object descendants at 0x000002A2AD9E86D0>
for i,child in enumerate(soup.p.descendants):
print(i,child)
# 0 <b>The Dormouse's story</b>
# 1 The Dormouse's story

父节点和祖先节点

parent来获取父节点

soup = BeautifulSoup(html, 'lxml')
print(soup.b.parent) # <p class="title"><b>The Dormouse's story</b></p>

parents来获取祖先节点,是一个迭代器的形式

soup = BeautifulSoup(html, 'lxml')
print(list(soup.b.parents))

兄弟节点

next_siblings获取后面的全部的兄弟节点,返回一个列表

soup = BeautifulSoup(html, 'lxml')
print(list(soup.p.next_siblings))

previous_siblings获取前面的全部的兄弟节点,返回一个列表

soup = BeautifulSoup(html, 'lxml')
print(list(soup.p.previous_siblings))

find_all()

find_all( name , attrs , recursive , text , **kwargs )

bs4中最常用的方法,find_all()方法返回满足条件的全部标签,find()方法只返回第一个标签。

返回结果是一个列表,每一个元素都是一个tag对象,可以使用get_text()来获取标签的文本。

  • name 参数

    可以查找所有名字为 name 的tag,字符串对象会被自动忽略掉.
soup.find_all("title")
# [<title>The Dormouse's story</title>]
  • keyword 参数

    如果一个指定名字的参数不是搜索内置的参数名,搜索时会把该参数当作指定名字tag的属性来搜索

    搜索指定名字的属性时可以使用的参数值包括 字符串 , 正则表达式 , 列表, True .

    为True时,相当于选取全部的带有该属性的标签,不论属性值是多少。
soup.find_all(id='link2')
# [<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>] soup.find_all(href=re.compile("elsie"))
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>]
  • attrs 参数

    有些tag属性在搜索不能使用,比如HTML5中的 data-* 属性, 这时候使用attrs参数定义一个字典参数来搜索包含特殊属性的tag:
data_soup.find_all(attrs={"data-foo": "value"})
# [<div data-foo="value">foo!</div>]
  • text参数

    通过 text 参数可以搜搜文档中的字符串内容.与 name 参数的可选值一样, text 参数接受 字符串 , 正则表达式 , 列表, True .
soup.find_all(text='foo')
soup.find_all("a", text="Elsie")
  • limit 参数

    使用 limit 参数限制返回结果的数量
soup.find_all("a", limit=2)
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
# <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]
  • recursive 参数

    调用tag的 find_all() 方法时,Beautiful Soup会检索当前tag的所有子孙节点,如果只想搜索tag的直接子节点,可以使用参数 recursive=False
soup.html.find_all("title", recursive=False)

CSS选择器

通过select()直接传入CSS选择器即可

soup.select("title")
# [<title>The Dormouse's story</title>] soup.select("p nth-of-type(3)")
# [<p class="story">...</p>]

BeautifulSoup解析库的更多相关文章

  1. BeautifulSoup解析库的介绍和使用

    ### BeautifulSoup解析库的介绍和使用 ### 三大选择器:节点选择器,方法选择器,CSS选择器 ### 使用建议:方法选择器 > CSS选择器 > 节点选择器 ## 测试文 ...

  2. 第三节:Web爬虫之BeautifulSoup解析库

    Beautiful Soup官方说明: Beautiful Soup提供一些简单的.python式的函数用来处理导航.搜索.修改分析树等功能.它是一个工具箱,通过解析文档为用户提供需要抓取的数据,因为 ...

  3. BeautifulSoup与Xpath解析库总结

    一.BeautifulSoup解析库 1.快速开始 html_doc = """ <html><head><title>The Dor ...

  4. xpath beautiful pyquery三种解析库

    这两天看了一下python常用的三种解析库,写篇随笔,整理一下思路.太菜了,若有错误的地方,欢迎大家随时指正.......(conme on.......) 爬取网页数据一般会经过 获取信息-> ...

  5. Python爬虫3大解析库使用导航

    1. Xpath解析库 2. BeautifulSoup解析库 3. PyQuery解析库

  6. 解析库-beautifulsoup模块

    # -*- coding: utf-8 -*- from bs4 import BeautifulSoup # 安装:pip install beautifulsoup4 # Beautiful So ...

  7. 爬虫 解析库re,Beautifulsoup,

    re模块 点我回顾 Beautifulsoup模块 #安装 Beautiful Soup pip install beautifulsoup4 #安装解析器 Beautiful Soup支持Pytho ...

  8. Python爬虫【解析库之beautifulsoup】

    解析库的安装 pip3 install beautifulsoup4 初始化 BeautifulSoup(str,"解析库") from bs4 import BeautifulS ...

  9. 解析库之re,Beautifulsoup

    本篇导航: 介绍 基本使用 遍历文档树 搜索文档树 总结     re模块在之前的python进阶中有讲过不再做过多的阐述,本篇为BeautifulSoup库的分析 20.collections模块和 ...

随机推荐

  1. 【CodeForces】913 F. Strongly Connected Tournament 概率和期望DP

    [题目]F. Strongly Connected Tournament [题意]给定n个点(游戏者),每轮游戏进行下列操作: 1.每对游戏者i和j(i<j)进行一场游戏,有p的概率i赢j(反之 ...

  2. zedboard学习记录.3.oled,创建IP

    环境:win7 .vivado 2017.4 .zedboard rev.d 首先建立工程. 1.Tools -> Create and Package New IP 2.Create AXI4 ...

  3. 树的直径(两个bfs)

    题目链接:https://cn.vjudge.net/problem/POJ-2631 树的直径:树中的最长链 具体思路:随便找一个点bfs,然后找到最长的链,然后再以找到的点作为起点进行bfs,然后 ...

  4. 2018 黑盾杯部分writeup

    这次比赛拿了三十多名.呵呵.或许这就是菜吧. 比赛经验教训: 1.赛前一定要调整好情绪. 2.比赛尽量不要紧张,每一题都认认真真的看过去! 3.站在出题者的角度去思考问题 4.刷题 5.还是情绪吧.其 ...

  5. Python排序算法之插入排序

    # 插入排序的工作原理是,对于每个未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入.## 步骤:## 从第一个元素开始,该元素可以认为已经被排序# 取出下一个元素,在已经排序的元素序列中从后 ...

  6. ACM International Collegiate Programming Contest World Finals 2013

    ACM International Collegiate Programming Contest World Finals 2013 A - Self-Assembly 题目描述:给出\(n\)个正方 ...

  7. 转:google测试分享-测试经理

    原文: http://blog.sina.com.cn/s/blog_6cf812be0102vode.html 前言:这个系列分享的内容大部分都是出自于<google是如何测试的>的书, ...

  8. Linux中常用命令 <一>

    本笔记中记录的命令来源于 <Linux C 编程实战> ------------------------------------------------------------------ ...

  9. Codefroces 735D Taxes(哥德巴赫猜想)

    题目链接:http://codeforces.com/problemset/problem/735/D 题目大意:给一个n,n可以被分解成n1+n2+n3+....nk(1=<k<=n). ...

  10. beego学习笔记(4):开发文档阅读(2)

    bee工具的安装和使用 bee 工具是一个为了协助快速开发 beego 项目而创建的项目,通过 bee 您可以很容易的进行 beego 项目的创建.热编译.开发.测试.和部署. go get gith ...