如果所说的“选定”是指第一个具有
selected
班
,您可以简单地测试:
nosto_sku_tags = bs4.select('select[name="id"] option')
for nosto_sku_tag in nosto_sku_tags:
if 'selected' in nosto_sku_tag.get('class', ()):
continue
sku_size = nosto_sku_tag.get_text(strip=True)
get_text()
方法)。
因为
class
,您需要使用
.get('class', ())
注意我用了
不同的查询
找到
<option>
在HTML中发布的元素。你的代码打印
<select>
元素,作为一个大对象,而不是个体
<选项>
<选择>
元素,然后
<选项>
CSS selector
直接进入选项。
:not(.selected)
psuedo选择器过滤掉这一个选项。
>>> nosto_sku_tags = soup.select('select[name="id"] option')
>>> for nosto_sku_tag in nosto_sku_tags:
... if 'selected' in nosto_sku_tag.get('class', ()):
... continue
... sku_size = nosto_sku_tag.get_text(strip=True)
... print(sku_size)
...
4,5
5,5
6
7
8,5
9
10
不
在
<
元素,而没有
挑选出来的
第一
for nosto_sku_tag in nosto_sku_tags[1:]:
sku_size = nosto_sku_tag.get_text(strip=True)
注意
[1:]
切片。