How to Beautiful Soup (bs4) 只匹配一个,而且只有一个,css 类 [英] How to Beautiful Soup (bs4) match just one, and only one, css class
问题描述
我正在使用以下代码来匹配所有具有 CSS 类ad_item"的 div.
soup.find_all('div',class_="ad_item")
我遇到的问题是,在该网页上,还有 div 的 CSS 类设置为ad_ex_item"和ad_ex_item".
<块引用>当您搜索与某个 CSS 类匹配的标签时,您会与其任何 CSS 类匹配:
那么如何匹配只有ad_item"而没有ad_ex_item"的 div.
或者换句话说,如何搜索只有 CSS 类ad_item"的 div?
解决方案我找到了一个解决方案,虽然与BS4无关,但是是纯python代码.
对于soup.find_all('div',class_="ad_item")中的项目:如果 len(item["class"]) != 1:继续;
它基本上跳过项目,如果有多个 CSS 类.
I am using following code to match all div that have CSS class "ad_item".
soup.find_all('div',class_="ad_item")
problem that I have is that, on that web page, there are also div that have CSS class set to "ad_ex_item" and "ad_ex_item".
<div class="ad_item ad_ex_item">
In documentation it is stated:
When you search for a tag that matches a certain CSS class, you’re
matching against any of its CSS classes:
So how can I match div, that have only "ad_item", and do not have "ad_ex_item".
Or to put this in another way, how to search for div that have only CSS class "ad_item" ?
解决方案 I have found one solution, although it have nothing to do with BS4, it is pure python code.
for item in soup.find_all('div',class_="ad_item"):
if len(item["class"]) != 1:
continue;
It basically skip item, if there is more than one CSS class.
这篇关于How to Beautiful Soup (bs4) 只匹配一个,而且只有一个,css 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文