博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
beautifulsoup之CSS选择器
阅读量:4694 次
发布时间:2019-06-09

本文共 3622 字,大约阅读时间需要 12 分钟。

BeautifulSoup支持大部分的CSS选择器,其语法为:向tag或soup对象的.select()方法中传入字符串参数,选择的结果以列表形式返回。

  tag.select("string")

  BeautifulSoup.select("string")

 

源代码示例:

html = """               The Dormouse's story                   

The Dormouse's story

Once upon a time there were three little sisters; and their names were the first b tag Elsie , Lacie and Tillie ;and they lived at the bottom of a well.

myStory the end a tag

the p tag sibling """
soup = BeautifulSoup(html,'lxml')

  1、通过标签选择

# 选择所有title标签soup.select("title")# 选择所有p标签中的第三个标签soup.select("p:nth-of-type(3)") 相当于soup.select(p)[2]# 选择body标签下的所有a标签soup.select("body a")# 选择body标签下的直接a子标签soup.select("body > a")# 选择id=link1后的所有兄弟节点标签soup.select("#link1 ~ .mysis")# 选择id=link1后的下一个兄弟节点标签soup.select("#link1 + .mysis")

  2、通过类名查找

# 选择a标签,其类属性为mysis的标签soup.select("a.mysis")

  3、通过id查找

# 选择a标签,其id属性为link1的标签soup.select("a#link1")

  4、通过【属性】查找,当然也适用于class

# 选择a标签,其属性中存在myname的所有标签soup.select("a[myname]")# 选择a标签,其属性href=http://example.com/lacie的所有标签soup.select("a[href='http://example.com/lacie']")# 选择a标签,其href属性以http开头soup.select('a[href^="http"]')# 选择a标签,其href属性以lacie结尾soup.select('a[href$="lacie"]')# 选择a标签,其href属性包含.comsoup.select('a[href*=".com"]')# 从html中排除某标签,此时soup中不再有script标签[s.extract() for s in soup('script')] # 如果想排除多个呢[s.extract() for s in soup(['script','fram']

  

  5、获取文本及属性

html_doc = """            The Dormouse's story        

The Dormouse's story

Once upon a time there were three little sisters; and their names were Elsie, Lacie and Tillie;

and they lived at the bottom of a well.

...

"""from bs4 import BeautifulSoup'''以列表的形式返回'''soup = BeautifulSoup(html_doc, 'html.parser')s = soup.select('p.story')s[0].get_text() # p节点及子孙节点的文本内容s[0].get_text("|") # 指定文本内容的分隔符s[0].get_text("|", strip=True) # 去除文本内容前后的空白print(s[0].get("class")) # p节点的class属性值列表(除class外都是返回字符串)

 

  6、UnicodeDammit.detwingle() 方法只能解码包含在UTF-8编码中的Windows-1252编码内容,

new_doc = UnicodeDammit.detwingle(doc)print(new_doc.decode("utf8"))# ☃☃☃“I like snowmen!”

在创建 BeautifulSoup 或 UnicodeDammit 对象前一定要先对文档调用 UnicodeDammit.detwingle() 确保文档的编码方式正确.如果尝试去解析一段包含Windows-1252编码的UTF-8文档,就会得到一堆乱码,比如: ☃☃☃“I like snowmen!”.

  

  7、其它:

html_doc = """            The Dormouse's story        

The Dormouse's story

Once upon a time there were three little sisters; and their names were Elsie, Lacie and Tillie;

and they lived at the bottom of a well.

...

"""from bs4 import BeautifulSoup'''以列表的形式返回'''soup = BeautifulSoup(html_doc, 'html.parser')soup.select('title') # title标签soup.select("p:nth-of-type(3)") # 第三个p节点soup.select('body a') # body下的所有子孙a节点soup.select('p > a') # 所有p节点下的所有a直接节点soup.select('p > #link1') # 所有p节点下的id=link1的直接子节点soup.select('#link1 ~ .sister') # id为link1的节点后面class=sister的所有兄弟节点soup.select('#link1 + .sister') # id为link1的节点后面class=sister的第一个兄弟节点soup.select('.sister') # class=sister的所有节点soup.select('[class="sister"]') # class=sister的所有节点soup.select("#link1") # id=link1的节点soup.select("a#link1") # a节点,且id=link1的节点soup.select('a[href]') # 所有的a节点,有href属性soup.select('a[href="http://example.com/elsie"]') # 指定href属性值的所有a节点soup.select('a[href^="http://example.com/"]') # href属性以指定值开头的所有a节点soup.select('a[href$="tillie"]') # href属性以指定值结尾的所有a节点soup.select('a[href*=".com/el"]') # 支持正则匹配

 

转载于:https://www.cnblogs.com/kongzhagen/p/6472746.html

你可能感兴趣的文章
java课堂笔记
查看>>
java字符串与基础混淆
查看>>
Python中的输入和输出
查看>>
Python爬虫系列-BeautifulSoup详解
查看>>
定义了重复的system.web.extensions/scripting/scriptResourceHandler怎么办
查看>>
对Markdown编辑器的学习经验分享
查看>>
第二篇:正则表达式
查看>>
装饰器
查看>>
bzoj 5090 组题
查看>>
SQL 多条记录分组合成一条数据
查看>>
IBM messed up *AGAIN* in their thinkpad: 0xA0000 -> 0x9F000
查看>>
python 两个文件夹里的文件名对比
查看>>
vc++2010如何新建项目并在控制台打印helloworld
查看>>
腾讯云centos7.2安装jdk1.7 tomcat7.0部署项目示例
查看>>
二叉树的层次遍历
查看>>
Java之IO流
查看>>
web service 部署
查看>>
CSS padding 属性
查看>>
Windows7 Questions
查看>>
数据库迁移工具
查看>>