原文地址:http://www.bugingcode.com/blog/beautiful_soup_select.html

select 的功能跟findfind_all 一样用来选取特定的标签,它的选取规则依赖于css,我们把它叫做css选择器,如果之前有接触过jquery ,可以发现select的选取规则和jquery有点像。

通过标签名查找

在进行过滤时标签名不加任何修饰,如下:

from bs4 import BeautifulSoup
import re html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><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/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>
</body>
</html>
""" soup = BeautifulSoup(html, "lxml")
print soup.select('p')

返回的结果如下:

[<p class="title" name="dromouse"><b>The Dormouse's story</b></p>, <p class="story">Once upon a time there were three little sisters; and their names were\n<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and\n<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>;\nand they lived at the bottom of a well.</p>]

通过结果可以看出,他返回的是一个数组,再继续看看数组里的元素是什么呢?

print type(soup.select('p')[0])

结果为:

<class 'bs4.element.Tag'>

清楚了返回的是bs4.element.Tag,这一点和find_all是一样的,select('p') 返回了 所有标签名为p的tag。

通过类名和id进行查找

在进行过滤时类名前加点,id名前加 #

print soup.select('.title')
print soup.select('#link2')

返回的结果为:

[<p class="title" name="dromouse"><b>The Dormouse's story</b></p>]
[<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]

通过属性查找

如果不是id或者是类名,是不是就不能进行过滤了?如果可以,该如何来表达,

print soup.select('[href="http://example.com/lacie"]')

选择hrefhttp://example.com/lacie 的tag。

组合查找

组合查找可以分为两种,一种是在一个tag中进行两个条件的查找,一种是树状的查找一层一层之间的查找。

第一种情况,如下所示:

print soup.select('a#link2')

选择标签名为aidlink2的tag。

输出的结果如下:

[<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]

另一种情况,如下:

body开始,在body里面查找 所有的 p,在所有的p 中查找 标签名为aid 为link2的tag,这样像树状一层一层的查找,在分析html结构是是非常常见的。层和层之间用空格分开。

print soup.select('body p a#link2')

结果如下:

[<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]

转载请标明来之:http://www.bugingcode.com/

更多教程:阿猫学编程

Beautiful Soup的用法(五):select的使用的更多相关文章

  1. Python之Beautiful Soup的用法

    1. Beautiful Soup的简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: Beautiful Soup提供一些简单的.pyt ...

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

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

  3. python爬虫(7)--Beautiful Soup的用法

    1.Beautiful Soup简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据. Beautiful Soup提供一些简单的.python式的函数用来 ...

  4. Beautiful Soup的用法

    BEAUTIFUL SOUP的介绍 就是一个非常好用.漂亮.牛逼的第三方库,是用Python写的一个HTML/XML的解析器,它可以很好的处理不规范标记并生成剖析树(parse tree). 它提供简 ...

  5. python 爬虫5 Beautiful Soup的用法

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

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

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

  7. 使用Beautiful Soup

    Beautiful Soup初了解 # 解析工具Beautiful Soup,借助网页的结构和属性等特性来解析网页(简单的说就是python的一个HTML或XML的解析库)# Beautiful So ...

  8. 推荐一些python Beautiful Soup学习网址

    前言:这几天忙着写分析报告,实在没精力去研究django,虽然抽时间去看了几遍中文文档,还是等实际实践后写几篇操作文章吧! 正文:以下是本人前段时间学习bs4库找的一些网址,在学习的可以参考下,有点多 ...

  9. Beautiful Soup库

    原文传送门:静觅 » Python爬虫利器二之Beautiful Soup的用法

随机推荐

  1. 基于node的前后端分离初识

    久闻node的大名,先后也看过node的文档,但是,总是碍于没有挑起我的G点,所以实际codeing的例子都没有.最近,突然很有兴致,想把原有用页面ajax渲染的页面采用服务端node来渲染,研究了两 ...

  2. 谷歌为何要研发新系统在5年内取代Android?

    现在的Android系统已经越做越好,体验也愈来愈佳,是唯一能和iOS掰腕子的移动操作系统.而且对于很多智能手机厂商来说,开源的Android为它们节约了太多成本,是不可或缺的基石之一.因此,想必很多 ...

  3. js运算符相关要点

    取模运算的结果符号只与左边值的符号有关: var x = 7 % 3; // 结果为 1 var y = 7 % (-3); // 结果为 1 var z = (-7) % 3; // 结果为 -1

  4. 反射(hasattr和setattr和delattr)

    反射(hasattr和setattr和delattr) 一.反射在类中的使用 反射就是通过字符串来操作类或者对象的属性, 反射本质就是在使用内置函数,其中反射有以下四个内置函数: hasattr:通过 ...

  5. Linux] Git: push 出错的解决 master -> master (branch is currently checked out)

      在使用Git Push代码到数据仓库时,提示如下错误: [remote rejected] master -> master (branch is currently checked out ...

  6. 画张自己能理解的dotnet core 微服务图

  7. webfrom 控件

    服务器基本控件: button: text属性 linkbutton:text属性,它是一个超链接模样的普通button hyperlink: navigateurl:链接地址,相当于<a> ...

  8. linux下 c语言调用c++

    /*****************************g++编译cpp 文件为库文件.编译C文件时gcc 要链接 -l stdc++ 这个库**(非常重要)*///定义c++ class 头文件 ...

  9. CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://repo.anaconda.com/pkgs/main/win-64/repodata.json.bz2> Elapsed: -

    将C:\Users\<本机用户名>\.condarc文件修改为 channels: - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/ ...

  10. logService

    vpackage org.linlinjava.litemall.admin.service; import org.apache.shiro.SecurityUtils; import org.ap ...