因此,我一直在尝试找出一些问题并继续学习。我创建了一个脚本,在其中打印出数字,但是其中一些数字具有禁用的类,而我想做的就是我只想打印出那些在“类”中没有禁用的数字
try:
select_tags = bs4.find_all('select', {'autocomplete': 'off'})
except Exception:
select_tags = []
data_products = []
for select_tag in select_tags:
test = select_tag.find_all('option')
for hehe in test[1:]:
print(hehe)
print("----------")
if hehe.select('select > option(.disabled)'):
print('Skip')
continue
try:
found = hehe.text
found = found.replace(" ", "")
found = found.replace("\n", "")
except Exception:
found = None
found_data = {
"found": found
}
data_products.append(found_data)
print(data_products)
输出:
----------
<option class="" value="11_141">
(Number 1) </option>
----------
<option class="" value="11_142">
(Number 2) </option>
----------
<option class="" value="11_143">
(Number 3) </option>
----------
<option class="disabled ReminderRevealButton" value="11_144">
(Number 4) </option>
----------
<option class="" value="11_145">
(Number 5) </option>
----------
<option class="disabled ReminderRevealButton" value="11_137">
(Number 6) </option>
----------
<option class="" value="11_136">
(Number 7) </option>
----------
我想要输出的是:
(Number 1)
(Number 2)
(Number 3)
#Skip number 4 because it has Disabled
(Number 5)
#Skip number 6 because it has disabled
(Number 7)
我需要做什么才能解决该问题?
答案 0 :(得分:2)
下面的代码跳过已禁用类的选项。
try:
select_tags = bs4.find_all('select', {'autocomplete': 'off'})
except Exception:
select_tags = []
data_products = []
for select_tag in select_tags:
test = select_tag.find_all('option')
for hehe in test[1:]:
if "disabled" not in hehe:
print(hehe.text)