美丽的汤不能找到一个CSS类,如果对象具有其他类,也 [英] Beautiful Soup cannot find a CSS class if the object has other classes, too

查看:207
本文介绍了美丽的汤不能找到一个CSS类,如果对象具有其他类,也的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果一个页面有< D​​IV CLASS =1级> < p类=1级> ,那么 soup.findAll(真,'1级')会找到他们。

if a page has <div class="class1"> and <p class="class1">, then soup.findAll(True, 'class1') will find them both.

如果有&LT; p =类class1的类class2&GT; ,虽然,它不会被发现。我如何找到与某一类的所有对象,无论他们是否有其他类,也?

If it has <p class="class1 class2">, though, it will not be found. How do I find all objects with a certain class, regardless of whether they have other classes, too?

推荐答案

万一有人遇到这个问题。 BeautifulSoup现在支持这样的:

Just in case anybody comes across this question. BeautifulSoup now supports this:

Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.

In [1]: import bs4

In [2]: soup = bs4.BeautifulSoup('<div class="foo bar"></div>')

In [3]: soup(attrs={'class': 'bar'})
Out[3]: [<div class="foo bar"></div>]

此外,您不必键入的findAll了。

Also, you don't have to type findAll anymore.

这篇关于美丽的汤不能找到一个CSS类,如果对象具有其他类,也的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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