gpt4 book ai didi

python - 获取BeautifulSoup中id为空的标签内容

转载 作者:太空宇宙 更新时间:2023-11-03 17:38:14 26 4
gpt4 key购买 nike

from bs4 import BeautifulSoup

page = """<span id="something">useless</span>
<span id="">some text</span>
<span id="different">useless</span>"""
soup = BeautifulSoup(page)

如何才能仅获取一些文本?使用 soup.find_all('span', {'id': ""}) 查找所有内容。

最佳答案

您有两个选择:

  1. 使用自定义过滤器;传入一个函数,它会被要求为元素返回 TrueFalse:

    soup.find_all(lambda e: e.name == 'span' and e.attrs.get('id') == '')
  2. 使用CSS selector ,具有精确的属性匹配:

    soup.select('span[id=""]')

演示:

>>> from bs4 import BeautifulSoup
>>> page = """<span id="something">useless</span>
... <span id="">some text</span>
... <span id="different">useless</span>"""
>>> soup = BeautifulSoup(page)
>>> soup.find_all(lambda e: e.name == 'span' and e.attrs.get('id') == '')
[<span id="">some text</span>]
>>> soup.select('span[id=""]')
[<span id="">some text</span>]

关于python - 获取BeautifulSoup中id为空的标签内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30916180/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com