Beautiful Soup的用法(五):select的使用
原文地址:http://www.bugingcode.com/blog/beautiful_soup_select.html
select 的功能跟find和find_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"]')
选择href 为http://example.com/lacie 的tag。
组合查找
组合查找可以分为两种,一种是在一个tag中进行两个条件的查找,一种是树状的查找一层一层之间的查找。
第一种情况,如下所示:
print soup.select('a#link2')
选择标签名为a,id为link2的tag。
输出的结果如下:
[<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]
另一种情况,如下:
从body开始,在body里面查找 所有的 p,在所有的p 中查找 标签名为a,id 为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的使用的更多相关文章
- Python之Beautiful Soup的用法
1. Beautiful Soup的简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: Beautiful Soup提供一些简单的.pyt ...
- Python爬虫利器二之Beautiful Soup的用法
上一节我们介绍了正则表达式,它的内容其实还是蛮多的,如果一个正则匹配稍有差池,那可能程序就处在永久的循环之中,而且有的小伙伴们也对写正则表达式的写法用得不熟练,没关系,我们还有一个更强大的工具,叫Be ...
- python爬虫(7)--Beautiful Soup的用法
1.Beautiful Soup简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据. Beautiful Soup提供一些简单的.python式的函数用来 ...
- Beautiful Soup的用法
BEAUTIFUL SOUP的介绍 就是一个非常好用.漂亮.牛逼的第三方库,是用Python写的一个HTML/XML的解析器,它可以很好的处理不规范标记并生成剖析树(parse tree). 它提供简 ...
- python 爬虫5 Beautiful Soup的用法
1.创建 Beautiful Soup 对象 from bs4 import BeautifulSoup html = """ <html><head& ...
- python爬虫之Beautiful Soup的基本使用
1.简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: Beautiful Soup提供一些简单的.python式的函数用来处理导航.搜索 ...
- 使用Beautiful Soup
Beautiful Soup初了解 # 解析工具Beautiful Soup,借助网页的结构和属性等特性来解析网页(简单的说就是python的一个HTML或XML的解析库)# Beautiful So ...
- 推荐一些python Beautiful Soup学习网址
前言:这几天忙着写分析报告,实在没精力去研究django,虽然抽时间去看了几遍中文文档,还是等实际实践后写几篇操作文章吧! 正文:以下是本人前段时间学习bs4库找的一些网址,在学习的可以参考下,有点多 ...
- Beautiful Soup库
原文传送门:静觅 » Python爬虫利器二之Beautiful Soup的用法
随机推荐
- usert
usert类型 不是一个函数,而是一个语言构造器 usert后会不会释放内存 当usert的文件大于2044KB时才会释放内存,否则不释放内存
- Python筛法求素数
l=[2]m,n=input().split()m=int(m)n=int(n) for i in range(m,n): flag=True for j in l: if i%j==0:#如果当前值 ...
- Java clone方法的使用
浅克隆 Person p2 = (Person) p1.clone(); clone()方法使用后得到p2,p2和p1指向不同的地址.但是如果p1中的属性是引用类型,那么不再对这个引用类型进行复制,而 ...
- keras字符编码
https://www.jianshu.com/p/258a21ae0390https://blog.csdn.net/apengpengpeng/article/details/80866034#- ...
- grub.cfg文件编辑
grub2启动项里面找不到Windows的情况,这时候就需要自己去配置grub.cfg 在grub.cfg中加入如下代码: menuentry 'Windows Boot Manager (on /d ...
- javascript获取数组最后一个元素(三种方法)
JavaScript 获取Array末尾元素 一.JavaScript pop() 方法 pop() 方法用于删除并返回数组的最后一个元素. 注意:pop() 方法将删除 arrayObject 的最 ...
- [原]livekd使用问题记录
sysinternal suite中的livekd.exe可谓神器.可以用来观察本地内核的一些状态,当然抓内核dump再合适不过了. 在使用livekd的时候遇到了一些问题,现总结如下: 使用live ...
- jmeter接口自动化测试,数据驱动玩法
总体思路:excel管理测试数据,判断不同的接口请求方法,取登陆token值为全局变量方便后面接口调用,预期结果断言: 1.设置获取excel数据源: 2.设置取token以及设置为全局变量: 3.i ...
- Maven中settings.xml文件各标签含义
原文地址:http://www.cnblogs.com/jingmoxukong/p/6050172.html?utm_source=gold_browser_extension settings.x ...
- HDU-1540 Tunnel Warfare(区间连续点长度)
http://acm.hdu.edu.cn/showproblem.php?pid=1540 Time Limit: 4000/2000 MS (Java/Others) Memory Limi ...