soup.select()函数的使用用法

soup.select()在源代码中的原型为:

select(self, selector, namespaces=None, limit=None, **kwargs)

功能:查找html中我们所需要的内容
我们主要使用的参数是selector,其定义为”包含CSS选择器的字符串“。关于CCS,也需要了解一些概念,参考CCS语法CSS Id 和 Class

我们在写 CSS 时,标签名不加任何修饰,类名前加点,id名前加 #,在这里我们也可以利用类似的方法来筛选元素,用到的方法是 soup.select()。如下代码(如h1,p等为标签.center为类名,#para1为id):

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>菜鸟教程(runoob.com)</title> 
<style>
.center
{
	text-align:center;
}
#para1
{
	text-align:center;
	color:red;
} 
</style>
</head>

<body>
<h1 class="center">标题居中</h1>
<p id="para1">红色段落居中。</p> 
</body>
</html>

以下是将要用到的代码

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 class="sister" href="http://example.com/elsie" id="link1"> <!-- Elsie --> </a>,
			   <a class="sister" href="http://example.com/lacie" id="link2"> Lacie </a>and
			   <a class="sister" href="http://example.com/tillie" id="link3"> Tillie </a>;
			   and they lived at the bottom of a well.
		  </p>
		  <p class="story"> ... </p>
	 </body>
</html>
"""
soup = BeautifulSoup(html, 'lxml')  # 传入解析器:lxml

soup.select()可通过以下方法进行查找。

1.通过(HTML)标签名查找

print(soup.select('title'))
print(soup.select('a'))#输出的列表含有多个包含标签<a>的元素

输出为:

[<title>The Dormouse's story</title>]
[<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.通过CCS类选择器查找

print(soup.select('.story'))

输出为

[<p class="story">Once upon a time there were three little sisters; and their names were
<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>,
<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>, 
<p class="story">...</p>]

3.通过CCS id 选择器查找

print(soup.select('#link1'))

输出为:

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

4.组合查找

组合查找和通过标签名与类选择器、id选择器进行查找的组合原理是一样的,例如查找 p 标签中,id 等于 link1的内容,二者需要用空格分开。

print(soup.select('p #link1'))

输出为:

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

5.子标签查找

父标签与子标签之间通过" > "表示递进关系

print(soup.select('p > b'))

输出为:

[<b>The Dormouse's story</b>]

通过子标签查找时的注意事项:soup.select()尽量不使用完整selector

6.通过属性查找

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

# 通过href属性及其值进行查找
print(soup.select('a[href="http://example.com/elsie"]'))

输出为:

[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]
  • 33
    点赞
  • 182
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: Python爬虫中的soup.select函数是一个基于CSS选择器的方法,用于从HTML或XML文档中选择元素。它可以根据标签名、类名、id、属性等多种方式进行选择,返回一个元素列表。使用函数可以方便地提取网页中的数据,是Python爬虫中常用的工具之一。 ### 回答2: Python爬虫是指使用Python语言编写程序,模拟浏览器的行为访问网站,并从中获取有用的数据或信息的技术。Python爬虫采用的是解析网页的方式来获取信息,其中就包括显式解析和隐式解析,而soup.select()函数就是在显式解析中用于查找HTML文档中特定的标签或属性的函数soup.select()函数的作用是在页面中选择一个或多个CSS选择器标签,并返回一个结果集合,即BeautifulSoup对象或Tag对象的列表。该函数的输入参数是CSS选择器,可以是标签选择器,属性选择器、类选择器等。 首先,需要安装BeautifulSoup库,并导入库中的BeautifulSoup类。然后,使用requests库向目标网站发起请求,将该网站获取到的内容传入BeautifulSoup类的构造方法中,生成一个BeautifulSoup对象。 接下来,使用soup.select()函数查找与CSS选择器匹配的标签,可以通过标签名称、类、属性等方式进行选择。例如,soup.select('div')选取页面中所有的div标签,soup.select('.class')选取所有带有class属性的标签,soup.select("#id")选取所有具有id属性的标签等。soup.select()函数返回的是CSS选择器选中的所有元素对象构成的列表。 soup.select()函数还可以用于提取标签中的文本内容或属性值。例如,soup.select('a')[0]['href']可以获取页面中第一个a标签的href属性值,soup.select('p')[0].text获取页面中第一个p标签中的文本内容等。 总之,soup.select()函数是Python爬虫中常用的函数之一,可以用于快速获取页面中的数据和信息,使得爬虫的代码编写更加简单和高效。 ### 回答3: Python爬虫是一种常见的数据采集技术,可以用于从网站上抓取数据并进行分析。Python的BeautifulSoup库是一种非常流行的爬虫库,可用于解析网页HTML内容。soup.select()函数是该库中常用的函数之一。 soup.select()函数可用于选择网页HTML中指定的元素。它使用CSS选择器来定位和筛选要抓取的内容。例如,如果想要抓取所有class为“title”的元素,可以使用以下代码: ```python from bs4 import BeautifulSoup import requests url = 'https://www.example.com' r = requests.get(url) soup = BeautifulSoup(r.content, 'html.parser') titles = soup.select('.title') ``` 在以上代码中,首先通过requests库获取网页内容,然后使用BeautifulSoup库解析HTML。最后,使用soup.select()函数选取所有class为“title”的元素。选取结果返回的是一个列表,其中每个元素都是一个包含所选元素的Tag对象。 除了使用class来筛选元素,还可以使用标签名、ID、属性等CSS选择器来选取元素。例如,以下代码使用标签名选取了HTML中所有的div元素: ```python divs = soup.select('div') ``` 在使用soup.select()函数时,需要注意元素筛选的正确性和效率。如果选取的元素过多,可能会导致程序运行缓慢或卡死,所以需要谨慎使用。另外,如果需要抓取的HTML内容中包含动态内容,可能需要使用selenium等工具来模拟浏览器行为,先渲染出动态内容后再进行爬取。 总之,尽管在爬虫数据时需要一定的技术和经验,但对于我们来说,了解soup.select()函数是Python爬虫的重要一步。它是一种非常常用和有用的函数,用于从网页中选择和提取所需的内容。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值