如何通过Python的Beautiful Soup在班级和班级名称中找到带有空格的文本? [英] How can I find text in class and class name having spaces through Beautiful Soup in Python?

查看:251
本文介绍了如何通过Python的Beautiful Soup在班级和班级名称中找到带有空格的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我在<div>中有一个类,并且有空格.

For example I have a class in a <div> and a class having spaces.

<div class="class name having spaces"> </div>

推荐答案

from bs4 import BeautifulSoup
doc ='''<div class='the value'><\div>'''
soup = BeautifulSoup(doc, 'lxml')

print(soup.find_all(attrs={'class':'the value'}))
print(soup.find_all(class_='the value'))
# if the part of value is uniqu, you can use them separately
print(soup.find_all(class_='the'))        
print(soup.find_all(class_='value'))

退出:

[<div class="the value"></div>]
[<div class="the value"></div>]
[<div class="the value"></div>]
[<div class="the value"></div>]

这篇关于如何通过Python的Beautiful Soup在班级和班级名称中找到带有空格的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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