python爬虫之BeautifulSoup 使用select方法详解

 更新时间:2017年10月23日 14:38:40   作者:古怪的一阵风  
本篇文章主要介绍了python爬虫之BeautifulSoup 使用select方法详解,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文介绍了python爬虫之BeautifulSoup 使用select方法详解 ,分享给大家。具体如下:

<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/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link1"><!-- Elsie --></a>,
<a href="http://example.com/lacie" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""

我们在写 CSS 时,标签名不加任何修饰,类名前加点,id名前加 #,在这里我们也可以利用类似的方法来筛选元素,用到的方法是 soup.select(),返回类型是 list

(1)通过标签名查找

print soup.select('title') 
#[<title>The Dormouse's story</title>]
 
print soup.select('a')
#[<a class="sister" href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1"><!-- Elsie --></a>, <a class="sister" href="http://example.com/lacie" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link3">Tillie</a>]
 
print soup.select('b')
#[<b>The Dormouse's story</b>]

(2)通过类名查找

print soup.select('.sister')
#[<a class="sister" href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1"><!-- Elsie --></a>, <a class="sister" href="http://example.com/lacie" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link3">Tillie</a>]

(3)通过 id 名查找

print soup.select('#link1')
#[<a class="sister" href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1"><!-- Elsie --></a>]

(4)组合查找

组合查找即和写 class 文件时,标签名与类名、id名进行的组合原理是一样的,例如查找 p 标签中,id 等于 link1的内容,二者需要用空格分开

print soup.select('p #link1')
#[<a class="sister" href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1"><!-- Elsie --></a>]

直接子标签查找

print soup.select("head > title")
#[<title>The Dormouse's story</title>]

(5)属性查找

查找时还可以加入属性元素,属性需要用中括号括起来,注意属性和标签属于同一节点,所以中间不能加空格,否则会无法匹配到。

print soup.select("head > title")
#[<title>The Dormouse's story</title>]
 
print soup.select('a[href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ]')
#[<a class="sister" href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1"><!-- Elsie --></a>]

同样,属性仍然可以与上述查找方式组合,不在同一节点的空格隔开,同一节点的不加空格

print soup.select('p a[href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ]')
#[<a class="sister" href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1"><!-- Elsie --></a>]

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • python连接clickhouse数据库的两种方式小结

    python连接clickhouse数据库的两种方式小结

    这篇文章主要介绍了python连接clickhouse数据库的两种方式小结,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05
  • python格式化输出实例(居中、靠右及靠左对齐)

    python格式化输出实例(居中、靠右及靠左对齐)

    所谓格式化输出就是数据按照某种特殊的格式和要求进行输出,下面这篇文章主要给大家介绍了关于python格式化输出(居中、靠右及靠左对齐)的相关资料,文中介绍了format方式、其他扩展写法以及'%'方式,需要的朋友可以参考下
    2022-04-04
  • python35种绘图函数详细总结

    python35种绘图函数详细总结

    Python有许多用于绘图的函数和库,比如Matplotlib,Plotly,Bokeh,Seaborn等,这只是一些常用的绘图函数和库,Python还有其他绘图工具,如Pandas、ggplot等,选择适合你需求的库,可以根据你的数据类型、图形需求和个人偏好来决定,本文给大家总结了python35种绘图函数
    2023-08-08
  • 基于Python编写端口进程管理工具

    基于Python编写端口进程管理工具

    这篇文章主要为大家介绍了如何使用Python编写一个用于端口管理和进程管理的GUI工具,它可以显示当前系统上所有开放的端口信息,感兴趣的可以了解下
    2025-01-01
  • 怎样使用Python脚本日志功能

    怎样使用Python脚本日志功能

    Python通过logging模块提供日志功能,关于logging模块的使用网络上已经有很多详细的资料,今天这里要分享的是怎样在实际工程中使用日志功能。下面一起来学习学习。
    2016-08-08
  • 使用Selenium破解新浪微博的四宫格验证码

    使用Selenium破解新浪微博的四宫格验证码

    今天小编就为大家分享一篇关于使用Selenium破解新浪微博的四宫格验证码的文章,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-10-10
  • python类型强制转换long to int的代码

    python类型强制转换long to int的代码

    python的int型最大值和系统有关,32位和64位系统结果是不同的,分别为2的31次方减1和2的63次方减1,可以通过sys.maxint查看此值
    2013-02-02
  • 你应该知道的Python3.6、3.7、3.8新特性小结

    你应该知道的Python3.6、3.7、3.8新特性小结

    这篇文章主要介绍了你应该知道的Python3.6、3.7、3.8新特性小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • Python中的pyecharts库使用总结

    Python中的pyecharts库使用总结

    这篇文章主要介绍了Python中的pyecharts库使用总结,Pyecharts 提供了一个简单而直观的 API 接口,使得使用者无需了解复杂的 JavaScript 语法,即可通过 Python 代码实现高度定制化的图表设计,需要的朋友可以参考下
    2023-12-12
  • python实操案例练习(八)

    python实操案例练习(八)

    这篇文章主要介绍了python实操案例练习,本篇文章主要分享的案例内容有记录用户登录日志、模拟淘宝客服自动回复,下面详细的内容,需要的小伙伴可以参考一下。希望对你有所帮助
    2022-02-02

最新评论