Bs4 select_one vs find [英] Bs4 select_one vs find

查看:15
本文介绍了Bs4 select_one vs find的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道执行 bs.find('div')bs.select_one('div') 之间有什么区别.find_allselect 也是如此.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆