所以我一直在努力找出刮痧的方法并且仍在学习。我创建了一个脚本,它打印出数字,但有些数字有一个被禁用的类,我想要做的是我只想打印出那些在“类”中没有禁用的数字
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)
我需要做什么才能解决这个问题?
下面的代码跳过了禁用类的选项。
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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句