当前位置: 首页 > Python编程 > Python编程实战技能 > Python编程基础入门 > Python爬虫之CSS选择器

Python爬虫之CSS选择器

发布时间:2020年09月27日 09:54:08 来源: 点击量:429

【摘要】CSS选择器这是另一种与find_all()方法有异曲同工的查找方法,写CSS时,标签名不加任何修饰,类名前加 ,id名前加 。在这里我们也可以利用类

CSS选择器

这是另一种与find_all()方法有异曲同工的查找方法,写CSS时,标签名不加任何修饰,类名前加.,id名前加#。

在这里我们也可以利用类似的方法来筛选元素,用到的方法是soup.select(),返回的类型是list。

(1)通过标签名查找

#!/usr/bin/python3
# -*- coding:utf-8 -*-
 
from bs4 import BeautifulSoup
 
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/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>
"""
 
# 创建 Beautiful Soup 对象,指定lxml解析器
soup = BeautifulSoup(html, "lxml")
 
print(soup.select("title"))
 
print(soup.select("b"))
 
print(soup.select("a"))

运行结果

[<title>The Dormouse's story</title>]
[<b>The Dormouse's story</b>]
[<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>]

(2)通过类名查找

#!/usr/bin/python3
# -*- coding:utf-8 -*-
 
from bs4 import BeautifulSoup
 
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/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>
"""
 
# 创建 Beautiful Soup 对象,指定lxml解析器
soup = BeautifulSoup(html, "lxml")
 
print(soup.select(".title"))

运行结果

[<p class="title" name="dromouse"><b>The Dormouse's story</b></p>]

(3)通过id名查找

#!/usr/bin/python3
# -*- coding:utf-8 -*-
from bs4 import BeautifulSoup
 
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/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>
"""
 
# 创建 Beautiful Soup 对象,指定lxml解析器
soup = BeautifulSoup(html, "lxml")
 
print(soup.select("#link1"))

运行结果

[<p class="title" name="dromouse"><b>The Dormouse's story</b></p>]

相关推荐:《Python视频教程》

(4)组合查找

#!/usr/bin/python3
# -*- coding:utf-8 -*-
 
from bs4 import BeautifulSoup
 
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/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>
"""
 
# 创建 Beautiful Soup 对象,指定lxml解析器
soup = BeautifulSoup(html, "lxml")
 
print(soup.select("p #link1"))

运行结果

[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]

(5)属性查找

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

#!/usr/bin/python3
# -*- coding:utf-8 -*-
 
from bs4 import BeautifulSoup
 
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/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>
"""
 
# 创建 Beautiful Soup 对象,指定lxml解析器
soup = BeautifulSoup(html, "lxml")
 
print(soup.select("a[class='sister']"))

运行结果

[<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>]

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

#!/usr/bin/python3
# -*- coding:utf-8 -*-
 
from bs4 import BeautifulSoup
 
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/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>
"""
 
# 创建 Beautiful Soup 对象,指定lxml解析器
soup = BeautifulSoup(html, "lxml")
 
print(soup.select("p a[class='sister']"))

运行结果

[<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>]

(6)获取内容

以上的select()方法返回的结果都是列表形式,可以遍历形式输出,然后用get_text()方法来获取它的内容。

#!/usr/bin/python3
# -*- coding:utf-8 -*-
 
from bs4 import BeautifulSoup
 
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/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>
"""
 
# 创建 Beautiful Soup 对象,指定lxml解析器
soup = BeautifulSoup(html, "lxml")
 
print(soup.select("p a[class='sister']"))
 
for item in soup.select("p a[class='sister']"):
    print(item.get_text())

运行结果

[<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>]
 
Lacie
Tillie

注意:<!-- Elsie -->为注释内容,未输出

分享到: 编辑:wangmin

就业培训申请领取
您的姓名
您的电话
意向课程
点击领取

环球青藤

官方QQ

扫描上方二维码或点击一键加群,免费领取大礼包,加群暗号:青藤。 一键加群

绑定手机号

应《中华人民共和国网络安全法》加强实名认证机制要求,同时为更加全面的体验产品服务,烦请您绑定手机号.

预约成功

本直播为付费学员的直播课节

请您购买课程后再预约

环球青藤移动课堂APP 直播、听课。职达未来!

安卓版

下载

iPhone版

下载
环球青藤官方微信服务平台

刷题看课 APP下载

免费直播 一键购课

代报名等人工服务

课程咨询 学员服务 公众号

扫描关注微信公众号

APP

扫描下载APP

返回顶部