人生苦短,我用 Python

前文传送门:

小白学 Python 爬虫(1):开篇

小白学 Python 爬虫(2):前置准备(一)基本类库的安装

小白学 Python 爬虫(3):前置准备(二)Linux基础入门

小白学 Python 爬虫(4):前置准备(三)Docker基础入门

小白学 Python 爬虫(5):前置准备(四)数据库基础

小白学 Python 爬虫(6):前置准备(五)爬虫框架的安装

小白学 Python 爬虫(7):HTTP 基础

小白学 Python 爬虫(8):网页基础

小白学 Python 爬虫(9):爬虫基础

小白学 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 爬虫(19):Xpath 基操

小白学 Python 爬虫(20):Xpath 进阶

小白学 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 的属性来搜索,比如下面的示例我们直接搜索 idlink 的节点和 classtitle 的节点:

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 上,方便大家取用。

示例代码-Github

示例代码-Gitee

参考

https://beautifulsoup.readthedocs.io/zh_CN/v4.4.0/#

小白学 Python 爬虫(22):解析库 Beautiful Soup(下)的更多相关文章

  1. python爬虫之解析库Beautiful Soup

    为何要用Beautiful Soup Beautiful Soup是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式, 是一个 ...

  2. 小白学 Python 爬虫(21):解析库 Beautiful Soup(上)

    小白学 Python 爬虫(21):解析库 Beautiful Soup(上) 人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前 ...

  3. 小白学 Python 爬虫(23):解析库 pyquery 入门

    人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...

  4. 小白学 Python 爬虫(32):异步请求库 AIOHTTP 基础入门

    人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...

  5. 小白学 Python 爬虫(25):爬取股票信息

    人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...

  6. 小白学 Python 爬虫(26):为啥上海二手房你都买不起

    人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...

  7. 小白学 Python 爬虫(29):Selenium 获取某大型电商网站商品信息

    人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...

  8. 小白学 Python 爬虫(30):代理基础

    人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...

  9. 小白学 Python 爬虫(31):自己构建一个简单的代理池

    人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...

随机推荐

  1. 11.13的C++##不想写结构,更不爱指针

    //2019.11.13 卑微的Loving-Q瞎写的程序 报错请更改VS中的SDL检查// 我要去嗨了,在线卑微 1 #include<iostream> #include<std ...

  2. Tarjan-割点

    割点——tarjan #include <bits/stdc++.h> using namespace std; ; ; int n, m; int ans;//个数 * MAXM], n ...

  3. vuejs中的回车事件

    @keyup.enter.native="事件名称"

  4. Relation-Shape Convolutional Neural Network for Point Cloud Analysis(CVPR 2019)

    代码:https://github.com/Yochengliu/Relation-Shape-CNN 文章:https://arxiv.org/abs/1904.07601 作者直播:https:/ ...

  5. 【NOIP2017】【Luogu P3956】【SPFA】棋盘

    Luogu P3956 本题是一道简单的SPFA 具体看程序 #include<iostream> #include<cstdio> using namespace std; ...

  6. Glibc编译报错:*** LD_LIBRARY_PATH shouldn't contain the current directory when*** building glibc. Please change the environment variable

    执行glibc编译出错如下图 [root@localhost tmpdir]# ../configure --prefix=/usr/loacl/glibc2.9 --disable-profile ...

  7. 转载-FileZilla Server源码分析(1)

    FileZilla Server源码分析(1) 分类: VC 2012-03-27 17:32 2363人阅读 评论(0) 收藏 举报 serversocketftp服务器usersockets工作 ...

  8. Servlet+Ajax实现搜索框智能提示

    简介:搜索框相信大家都不陌生,几乎每天都会在各类网站进行着搜索.有没有注意到,很多的搜索功能,当输入内容时,下面会出现提示.这类提示就叫做搜索框的智能提示,本门课程就为大家介绍如何使用Servlet和 ...

  9. jenkins + maven + nexus + [ svn 或 GitLab 或 GitHub ]

    目录 介绍 DevOps平台四大模块 针对DevOps开源项目 Jenkins 介绍 Maven 介绍 maven的核心概念介绍 SVN介绍 Nexus介绍 Maven私服的 个特性: 流程图 环境搭 ...

  10. plot()与dev 函数族的使用

    1. plot()的类型选择 plot()函数,属于graphics包中. plot(a,b,type="value") type:以什么样的形式来画a,b之间的关系: ·&quo ...