gpt4 book ai didi

python - 如何用漂亮的汤跳过

转载 作者:太空宇宙 更新时间:2023-11-04 07:56:12 24 4
gpt4 key购买 nike

这是我的代码的输出

<h1 class="it-ttl" id="itemTitle" itemprop="name"><span class="g-hdn">Details about   </span>item name goes here</h1>

我只想获取项目名称,没有“详细信息”部分。

我的 Python 代码选择了特定的 div id 是

for content in soup.select('#itemTitle'):
print(content.text)

最佳答案

您可以使用 decompose() clear()extract() .根据文档:

Tag.decompose() removes a tag from the tree, then completely destroys it and its contents

Tag.clear() removes the contents of a tag

PageElement.extract() removes a tag or string from the tree. It returns the tag or string that was extracted

from bs4 import BeautifulSoup
html = '''<h1 class="it-ttl" id="itemTitle" itemprop="name"><span class="g-hdn">Details about </span>item name goes here</h1>'''

soup = BeautifulSoup(html, 'lxml')
for content in soup.select('#itemTitle'):
content.span.decompose()
print(content.text)

输出:

  item name goes here  

关于python - 如何用漂亮的汤跳过<span>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48414407/

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