教程集 www.jiaochengji.com
教程集 >  Python编程  >  Python入门  >  正文 Python爬虫之CSS选择器

Python爬虫之CSS选择器

发布时间:2021-12-11   编辑:jiaochengji.com
教程集为您提供Python爬虫之CSS选择器等资源,欢迎您收藏本站,我们将为您提供最新的Python爬虫之CSS选择器资源

CSS选择器

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

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

(1)通过标签名查找

<pre class="brush:php;toolbar:false">#!/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"))</pre>

运行结果

<pre class="brush:php;toolbar:false">[<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>]</pre>

(2)通过类名查找

<pre class="brush:php;toolbar:false">#!/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"))</pre>

运行结果

<pre class="brush:php;toolbar:false">[<p class="title" name="dromouse"><b>The Dormouse's story</b></p>]</pre>

(3)通过id名查找

<pre class="brush:php;toolbar:false">#!/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"))</pre>

运行结果

<pre class="brush:php;toolbar:false">[<p class="title" name="dromouse"><b>The Dormouse's story</b></p>]</pre>

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

(4)组合查找

<pre class="brush:php;toolbar:false">#!/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"))</pre>

运行结果

<pre class="brush:php;toolbar:false">[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]</pre>

(5)属性查找

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

<pre class="brush:php;toolbar:false">#!/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']"))</pre>

运行结果

<pre class="brush:php;toolbar:false">[<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>]</pre>

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

<pre class="brush:php;toolbar:false">#!/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']"))</pre>

运行结果

<pre class="brush:php;toolbar:false">[<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>]</pre>

(6)获取内容

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

<pre class="brush:php;toolbar:false">#!/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())</pre>

运行结果

<pre class="brush:php;toolbar:false">[<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</pre>

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

您可能感兴趣的文章:
Python 爬虫学习系列教程
经典必备之Python爬虫入门(一)
爬虫入门的基本原理,如果你连这些都不知道那你可以放弃爬虫了!
python的爬虫是什么意思
Python2爬虫入门之如何学习爬虫
Python写爬虫都用到什么库
Python3爬虫利器之pyquery的安装
python爬虫好学吗
Python2爬虫入门:爬虫基础知识
java爬虫没python爬虫好吗?

[关闭]
~ ~