python里面class有c-review__prefix和c-review__prefix c-review__prefix--color-green用eq分别判断有没有并且找到class怎么写
您可以使用BeautifulSoup库来解析HTML,并使用find_all方法来查找符合条件的元素,然后使用has_attr方法和eq方法来判断元素是否具有特定的class属性和特定的值。
下面是一个示例代码,假设您要查找class为c-review__prefix和c-review__prefix--color-green的元素:
from bs4 import BeautifulSoup
# 假设HTML代码已经存储在变量html中
soup = BeautifulSoup(html, 'html.parser')
# 查找所有class为c-review__prefix的元素
prefix_elements = soup.find_all(class_='c-review__prefix')
# 在所有符合条件的元素中查找class为c-review__prefix--color-green的元素
for prefix_element in prefix_elements:
if prefix_element.has_attr('class') and 'c-review__prefix--color-green' in prefix_element['class']:
# 找到符合条件的元素,执行对应的操作
# ...
在上面的代码中,prefix_elements是一个列表,包含了所有class为c-review__prefix的元素。然后,我们使用has_attr方法和eq方法来判断每个元素是否具有class为c-review__prefix--color-green的属性值。如果是,就执行对应的操作
原文地址: https://www.cveoy.top/t/topic/eChk 著作权归作者所有。请勿转载和采集!