Python - 如何在 bs4 中使用 select()

你好,那边

所以我一直在努力找出刮痧的方法并且仍在学习。我创建了一个脚本,它打印出数字,但有些数字有一个被禁用的类,我想要做的是我只想打印出那些在“类”中没有禁用的数字

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)

我需要做什么才能解决这个问题?

CodeIt

下面的代码跳过了禁用类的选项。

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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用python进行bs4过滤

使用bs4和Python从网页中提取

如何使用python和BS4从网页中正确提取和解析主题数据?

如何在bs4中使用以下分页方式抓取网站?

Python-如何使用bs4抓取JavaScript代码?

如何使用ID和名称查找隐藏的输入值-Python,bs4

如何在书本中使用新的bs4()主题?

如何使用bs4在python中抓取单页应用程序网站

无法使用Python BS4从页面查找文本

如何使用python和bs4修复Scrap Web Table输出CSV

如何使用python bs4获取Wikipedia表中的第一列值?

如何使用bs4或lxml获取在Python中找到XML标签的文本行?

使用bs4 python进行网页抓取:如何显示足球比赛

如何在bs4网址中使用扩展的ascii

如何使用bs4 / python从Wikipedia网站获取txt文件中单词的搜索结果?

Web Scraping,如何在python中使用bs4从两个相同的标签中提取数据

如何使用 Python BS4 访问 HTML <p> 标签内的文本

Python/BS4 - 在函数中使用列表作为输入

使用 bs4 解析 Python

如何使用python使用bs4进行网络抓取和访问<script>

如何使用 bs4/python 访问标头标签(即 <h1>)下方的标签

如何使用 Python 拆分 Javascript 代码 (bs4)

如何在 Python 上使用 bs4/selenium 从解析代码中创建 Pandas DataFrame?

如何使用 bs4 在 python 中获取带连字符的标签值?

如何在 Python 中使用 if 检查 bs4 元素的类型?

使用python bs4时如何从嵌套标签中获取信息?

如何在 python bs4 中使用 xpath 获取字符串?

如何在 python bs4 中使用 xpath 获取字符串?

Bs4 Python如何下载.css文件