Bs4 select_one vs find [英] Bs4 select_one vs find
问题描述
我想知道执行 bs.find('div')
和 bs.select_one('div')
之间有什么区别.find_all
和 select
也是如此.
I was wondering what is the difference between performing bs.find('div')
and bs.select_one('div')
. Same goes for find_all
and select
.
在性能方面是否有任何差异,或者在特定情况下是否有更好的使用.
Is there any difference performance wise, or if any is better to use over the other in specific cases.
推荐答案
select()
和 select_one()
为您提供了一种不同的方式来浏览 HTML 树,使用CSS 选择器,语法丰富且方便.虽然,BeautifulSoup
中的 CSS 选择器语法支持有限,但涵盖了最常见的情况.
select()
and select_one()
give you a different way navigating through an HTML tree using the CSS selectors which has rich and convenient syntax. Though, the CSS selector syntax support in BeautifulSoup
is limited but covers most common cases.
在性能方面,它实际上取决于要解析的 HTML 树以及要解析的元素、它的深度以及用于定位它的选择器.另外,find()
+ find_all()
与 select()
比较的替代方法也很重要.在像 bs.find('div')
与 bs.select_one('div')
这样的简单案例中,我会说,通常,find()
应该执行得更快,因为 在幕后支持 CSS 选择器语法的工作有很多.
Performance-wise, it really depends on an HTML tree to parse and on which element, how deep is it and what selector is used to locate it. Plus, what find()
+ find_all()
alternative there is to compare the select()
with, is also important. In a simple case like bs.find('div')
vs bs.select_one('div')
, I'd say that, generally, find()
should perform faster simply because there is a lot going on to support CSS selector syntax under-the-hood.
这篇关于Bs4 select_one vs find的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!