Python-bs4,检查类是否有值
所以我一直在玩bs4的游戏,我发现了一个我无法通过的问题。我想做的是,如果一个类中有值,那么我们通过,如果一个类中没有值,那么我们继续 情景一:Python-bs4,检查类是否有值,python,beautifulsoup,Python,Beautifulsoup,所以我一直在玩bs4的游戏,我发现了一个我无法通过的问题。我想做的是,如果一个类中有值,那么我们通过,如果一个类中没有值,那么我们继续 情景一: <option class="disabled RevealButton" value="1_120"> Overflow </option> <option class="disabled RevealButton"
<option class="disabled RevealButton" value="1_120">
Overflow </option>
<option class="disabled RevealButton" value="1_121">
Stack </option>
<option class="disabled RevealButton" value="1_122">
!!! </option>
它现在所做的是,它的印刷要么是第一种情况,要么是第二种情况
我希望输出的内容如下:
若类包含禁用的RevealButton,那个么我们只是传递并继续循环
如果类不包含“disabled RevealButton”,则我们打印出选择标签
我不知道我能做些什么来解决我的问题 要检查元素是否禁用了
和RevealButton
类,可以使用BeautifulSoup元素(标记
实例):
注意:您需要将其应用于option元素
请注意,class
是一个特殊的类,其值是一个列表
另一个选项(没有双关语)是查找两个类的option元素:
for select_tag in select_tags:
if select_tag.select("option.disabled.RevealButton"):
continue
print(select_tag)
这里,option.disabled.RevealButton
是一个匹配option
元素的函数,该元素同时具有disabled
和RevealButton
类。!因此,我尝试应用第二个选项,其中您有if-select\u-tag.select(“option.disabled.RevealButton”):
,但这只是告诉我AttributeError:'navigablesting'对象没有属性'select'
@hellosiroverter,因为您有这个select\u-tags=bs4.find('select',{'autocomplete':'off'))
它定位单个元素,当您迭代该元素时,它将迭代所有子元素,包括文本节点(可导航字符串)。你是说find_all()
?哦!是的,我想我忘了添加find_all!它现在似乎可以工作了我相信select
tag没有属性autocomplete
,我在html中看不到这个标签,你是说option
tag吗?
try:
select_tags = bs4.find('select', {'autocomplete': 'off'})
except Exception:
select_tags = []
for select_tag select_tags:
print(select_tag)
"disabled" in element["class"] and "RevealButton" in element["class"]
for select_tag in select_tags:
if select_tag.select("option.disabled.RevealButton"):
continue
print(select_tag)