如何在beautifulsoup中限制select标签的结果?

How to limit the result of select tag in beautifulsoup?

例如,我有这个:

result = soup.select('div#test > div.filters > span.text')

我想将上述列表的结果限制为 10 项。

find_all() 的情况下,可以使用 limit 参数,但是 select() 呢?

select() 没有 limit 参数,但您可以 切片 结果集:

soup.select('div#test > div.filters > span.text')[:10]

现在绝对有一个 limit 论点。

limit argument:

This works just like the LIMIT keyword in SQL. It tells Beautiful Soup to stop gathering results after it’s found a certain number.

适用于 find_allselect 方法。

例如,

result = soup.select('div#test > div.filters > span.text', limit = 10)