靓汤,用Findall()消除某些项目

2024-05-29 00:22:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一系列由find\u all()返回的列表项

<li class="rsltItem" mh-property-list-item="".......

该项有许多嵌套元素。在

有时,其中一个li稍有不同:

^{pr2}$

这个有较少的不同的嵌套元素。我要消除第二种类型。在

我尝试过在特定的类中搜索li,但是即使它们不同,它也会返回所有的值:

^{3}$

有什么办法可以从我的结果中排除第二个列表项吗?在


Tags: 元素类型列表propertyliallfinditem
2条回答
result_list = [tag for tag in soup.find_all('li', {'class': 'rsltItem'}) 
if tag['class'] == ['rsltItem']]

或者

^{pr2}$

要只查找与特定类rsltItem匹配的标记<li>

soup.find_all(lambda tag: tag.name == 'li' and tag.get('class') == ['rsltItem'])

相关问题 更多 >

    热门问题