小白学 Python 爬虫(22):解析库 Beautiful Soup(下)

人生苦短,我用 Python
前文传送门:
小白学 Python 爬虫(2):前置准备(一)基本类库的安装
小白学 Python 爬虫(3):前置准备(二)Linux基础入门
小白学 Python 爬虫(4):前置准备(三)Docker基础入门
小白学 Python 爬虫(6):前置准备(五)爬虫框架的安装
小白学 Python 爬虫(10):Session 和 Cookies
小白学 Python 爬虫(11):urllib 基础使用(一)
小白学 Python 爬虫(12):urllib 基础使用(二)
小白学 Python 爬虫(13):urllib 基础使用(三)
小白学 Python 爬虫(14):urllib 基础使用(四)
小白学 Python 爬虫(15):urllib 基础使用(五)
小白学 Python 爬虫(16):urllib 实战之爬取妹子图
小白学 Python 爬虫(17):Requests 基础使用
小白学 Python 爬虫(18):Requests 进阶操作
小白学 Python 爬虫(21):解析库 Beautiful Soup(上)
引言
前面一篇我们介绍的选择方法都是通过属性来进行选择的,这种方法使用起来非常简单,但是,如果 DOM 结构比较复杂的话,这种方法就不是那么友好了。
所以 Beautiful Soup 还为我们提供了一些搜索方法,如 find_all() 和 find() , DOM 节点不好直接用属性方法来表示,我们可以直接搜索嘛~~~
find_all()
先看下语法结构:
find_all( name , attrs , recursive , string , **kwargs )
find_all() 方法搜索当前 tag 的所有 tag 子节点,并判断是否符合过滤器的条件。
name
name 参数可以查找所有名字为 name 的 tag ,字符串对象会被自动忽略掉。
from bs4 import BeautifulSoup
html_doc = """
<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_doc, 'lxml')
print(soup.find_all(name = "a"))
print(type(soup.find_all(name = "a")[0]))
结果如下:
[<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>]
<class 'bs4.element.Tag'>
这次的示例换成了字符串,主要是为了各位同学看起来方便,再也不用去对照着图片看了。
这个示例我们使用了 find_all() 方法,并且传入了 name 参数,值为 a ,含义是我们要查找所有的 <a> 节点,可以看到,返回的结果数据类型是列表,长度为 3 ,并且元素类型为 bs4.element.Tag 。
因为元素类型为 bs4.element.Tag ,我们可以通过前一篇文章介绍的属性直接获取其中的内容:
for a in soup.find_all(name = "a"):
print(a.string)
结果如下:
Elsie
Lacie
Tillie
attrs
除了可以通过 name 进行搜索,我们还可以通过属性进行查询:
print(soup.find_all(attrs={'id': 'link1'}))
print(soup.find_all(attrs={'id': 'link2'}))
print(type(soup.find_all(attrs={'id': 'link1'})))
print(type(soup.find_all(attrs={'id': 'link2'})))
结果如下:
[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>]
[<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]
<class 'bs4.element.ResultSet'>
<class 'bs4.element.ResultSet'>
这个示例我们传入的是 attrs 参数,参数的数据类型是字典。
string
这个参数可用来匹配节点的文本,传入的形式可以是字符串,可以是正则表达式对象:
import re
print(soup.find_all(text=re.compile('sisters')))
结果如下:
['Once upon a time there were three little sisters; and their names were\n']
keyword
如果一个指定名字的参数不是搜索内置的参数名,搜索时会把该参数当作指定名字 tag 的属性来搜索,比如下面的示例我们直接搜索 id 为 link 的节点和 class 为 title 的节点:
print(soup.find_all(id='link1'))
print(soup.find_all(class_='title'))
结果如下:
[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>]
[<p class="title"><b>The Dormouse's story</b></p>]
当然,我们也可以使用多个指定名字的参数同时过滤 tag 的多个属性:
print(soup.find_all(href=re.compile("elsie"), id='link1'))
结果如下:
[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>]
有些 tag 属性在搜索不能使用,比如 HTML5 中的 data-* 属性,这时就需要用到上面介绍过的 attrs 参数了。
find()
find() 和 find_all() 非常的像,只不过 find() 不再像 find_all() 一样直接返回所有的匹配节点,而是只返回第一个匹配的元素。举几个简单的栗子:
print(soup.find(name = "a"))
print(type(soup.find(name = "a")))
结果如下:
<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
<class 'bs4.element.Tag'>
其余的查询方法各位同学可以参考官方文档,小编这里简单列举一下:
find_parents()和find_parent(): 用来搜索当前节点的父辈节点。find_next_siblings()和find_next_sibling(): 前者返回后面所有的兄弟节点,后者返回后面第一个兄弟节点。find_previous_siblings()和find_previous_sibling(): 前者返回前面所有的兄弟节点,后者返回前面第一个兄弟节点。find_all_next()和find_next():前者返回节点后所有符合条件的节点,后者返回第一个符合条件的节点。find_all_previous()和find_previous():前者返回节点后所有符合条件的节点,后者返回第一个符合条件的节点。
CSS
Beautiful Soup 除了提供前面这些属性选择、搜索方法等方式来获取节点,还提供了另外一种选择器 —— CSS 选择器。
如果对 CSS 选择器不熟的话,可以参考:https://www.w3school.com.cn/css/index.asp 。
使用 CSS 选择器方法非常简单,只需要调用 select() 方法,传入相应的 CSS 选择器即可,还是写几个简单的示例:
print(soup.select('#link1'))
print(type(soup.select('#link1')[0]))
print(soup.select('.story .sister'))
结果如下:
<class 'bs4.element.Tag'>
[<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>]
可以看到,我们使用 CSS 选择器获得的结果同样会是一个列表,并且里面的元素同样是 bs4.element.Tag ,这就意味着我们可以使用它的属性来获取对应的信息。
小结
Beautiful Soup 就这么简单的介绍完了,稍微做点小总结:
- 在选择解析器的时候尽量选择
lxml,官方推荐,据说是快。 - 节点属性筛选虽然简单但是功能有点弱鸡。
- find_all() 和 find() 其实可以很方便的帮助我们完成绝大多数的工作。
- CSS 选择器推荐有经验的同学使用,毕竟嘛,选择 DOM 节点,还是 CSS 选择器来的方便好使不是么?
示例代码
本系列的所有代码小编都会放在代码管理仓库 Github 和 Gitee 上,方便大家取用。
参考
https://beautifulsoup.readthedocs.io/zh_CN/v4.4.0/#
小白学 Python 爬虫(22):解析库 Beautiful Soup(下)的更多相关文章
- python爬虫之解析库Beautiful Soup
为何要用Beautiful Soup Beautiful Soup是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式, 是一个 ...
- 小白学 Python 爬虫(21):解析库 Beautiful Soup(上)
小白学 Python 爬虫(21):解析库 Beautiful Soup(上) 人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前 ...
- 小白学 Python 爬虫(23):解析库 pyquery 入门
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- 小白学 Python 爬虫(32):异步请求库 AIOHTTP 基础入门
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- 小白学 Python 爬虫(25):爬取股票信息
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- 小白学 Python 爬虫(26):为啥上海二手房你都买不起
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- 小白学 Python 爬虫(29):Selenium 获取某大型电商网站商品信息
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- 小白学 Python 爬虫(30):代理基础
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- 小白学 Python 爬虫(31):自己构建一个简单的代理池
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
随机推荐
- Ubuntu 16.04 安装Docker
1 更改apt源,更改前先对sources.list文件进行备分 ccskun@test:~$ sudo cp /etc/apt/sources.list /etc/apt/sources.list. ...
- PHP面向对象中new self( )和 new static( ) 的区别
在PHP中 self指向定义了当前被调用方法的类, static指向调用当前静态方法的类. class A { public static $_a = 'Class A'; public static ...
- nexus auto start
cd /etc/init.d ln -s /opt/nexus/nexus-2.3.1-01/bin/jsw/linux-x86-64/nexus nexus chkconfig --add nexu ...
- k8s 随记
1.kubelet参数解析:https://blog.csdn.net/qq_34857250/article/details/84995381 2.如何在github中查找k8s代码关键字? 现在我 ...
- PostGIS 结合Openlayers以及Geoserver实现最短路径分析(一)
环境: Win10 ArcMap10.4(用于数据处理) postgresql9.4 postgis2.2.3 pgRouting2.3(postgresql插件) ##附上本文配套素材下载地址:ht ...
- An end-to-end TextSpotter with Explicit Alignment and Attention
An end-to-end TextSpotter with Explicit Alignment and Attention 论文下载:http://cn.arxiv.org/pdf/1803.0 ...
- 品优购详情页---产品详细信息区域 iteminfo_wrap
产品详细信息区域为整个大盒子命名为: iteminfo_wrap 1号盒子命名为:sku_name 2号盒子命名为:news 3号盒子命名为:summary step1:3个盒子搭建框架,以及完成前两 ...
- mysql配置允许外界连接
1.在mysql的安装目录下找到mysql.ini 找到bind-address=127.0.0.1 ,(有的有,有的没有) 改为 bind-address = 0.0.0.0 2.登录mysql客户 ...
- centos 6.x 系统基础优化简版
Centos 6.x 系统基础优化 1.更换国内yum源 删除系统带的centos官方yum源 rm -rf /etc/yum.repos.d/* 使用国内阿里云源 curl -o /etc/yum. ...
- 【并发编程】Object的wait、notify和notifyAll方法
本博客系列是学习并发编程过程中的记录总结.由于文章比较多,写的时间也比较散,所以我整理了个目录贴(传送门),方便查阅. 并发编程系列博客传送门 方法简介 wait方法 wait方法是Object类中的 ...