Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
446 views
in Technique[技术] by (71.8m points)

python - Bs4 select_one vs find

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...