如何美丽汤(bs4)仅匹配css类中的一种 [英] How to Beautiful Soup (bs4) match just one, and only one, css class

查看:108
本文介绍了如何美丽汤(bs4)仅匹配css类中的一种的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码来匹配所有具有CSS类"ad_item"的div.

I am using following code to match all div that have CSS class "ad_item".

soup.find_all('div',class_="ad_item")

我遇到的问题是,在该网页上,还有div,其CSS类设置为"ad_ex_item"和"ad_ex_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">

在文档中指出:

当您搜索与某个CSS类匹配的标签时, 与其任何CSS类匹配:

When you search for a tag that matches a certain CSS class, you’re matching against any of its CSS classes:

那么我该如何匹配仅具有"ad_item"而没有"ad_ex_item"的div.

So how can I match div, that have only "ad_item", and do not have "ad_ex_item".

或者换一种说法,如何搜索仅具有CSS类"ad_item"的div?

Or to put this in another way, how to search for div that have only CSS class "ad_item" ?

推荐答案

我找到了一个解决方案,尽管它与BS4无关,但它是纯python代码.

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;

如果有多个CSS类,则基本上跳过该项目.

It basically skip item, if there is more than one CSS class.

这篇关于如何美丽汤(bs4)仅匹配css类中的一种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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