加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python-从标记中排除数据

发布时间:2020-12-17 17:41:03 所属栏目:Python 来源:网络整理
导读:我想在html span标签内排除特定文本.在下面给出的示例中,我只想从a-list-item下的class中的span中获取所有test2文本. 我的代码: span class="a-list-item"test1/spanspan class="a-list-item"test2/spanspan class="a-list-item"test2/span 我的代码:tag =

我想在html span标签内排除特定文本.在下面给出的示例中,我只想从a-list-item下的class中的span中获取所有test2文本.

我的代码:

<span class="a-list-item">test1</span>
<span class="a-list-item">test2</span>
<span class="a-list-item">test2</span>

我的代码:tag = tag.find_all(“ span”,{“ class”:“ a-list-item”})

如何仅获取所有test2.感谢您的答复

最佳答案
看来您使用的是美丽汤.在Beautiful Soup 4.7中,仅使用select而不是find_all即可轻松实现.您可以使用:not()包装的:contains()排除包含特定文本的跨度.

from bs4 import BeautifulSoup
markup = '''
<span class="a-list-item">test1</span> 
<span class="a-list-item">test2</span> 
<span class="a-list-item">test2</span>
'''
soup = BeautifulSoup(markup)
print(soup.select("span.a-list-item:not(:contains(test1))"))

输出量

[<span class="a-list-item">test2</span>,<span class="a-list-item">test2</span>]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读