簡體   English   中英

使用 Beautiful Soup 刪除特定標簽

[英]Remove a specific Tag with Beautiful Soup

我目前正在嘗試使用 python 和Beautiful Soup編寫腳本來更改 indigo 文件(.igs)中的相機,但我遇到了一個問題:

<scenedata>
   <tonemapping> <camera>...</camera> </tonemapping>
   <camera>...</camera>
</scenedata>

我只想刪除不在“色調映射”標簽內的“相機”標簽。

我試過soup.find('').replace _with 和soup.select('camera')但它總是刪除所有相機標簽。

只需檢查父母姓名並刪除您不需要的內容。

import bs4

text = """
<scenedata>
   <tonemapping> <camera>...</camera> </tonemapping>
   <camera>...</camera>
</scenedata>
"""
soup = bs4.BeautifulSoup(text, features="lxml")

for cam in soup.select("camera"):
    if cam.parent.name != "tonemapping":
        cam.extract()

您需要根據您的情況對其進行調整,但請嘗試以下方式:



    # build soup
    soup = BeautifulSoup(html_string, features='html.parser')
    
    # select first  tag
    map_tag = soup.select_one('tonemapping')
    
    # select the first  tag inside the selected  tag
    camera_tag = map_tag.select_one('camera')
    
    # remove the selected  tag
    camera_tag.decompose()

你可以試試這個。

from bs4 import BeautifulSoup

html_doc="""
<scenedata>
   <tonemapping> <camera>...</camera> </tonemapping>
   <camera>...</camera>
</scenedata>
"""

soup = BeautifulSoup(html_doc, 'html.parser')

s = soup.select_one('scenedata tonemapping > camera')
t = s.decompose()


print(soup)

Output

<scenedata>
<tonemapping>  </tonemapping>
<camera>...</camera>
</scenedata>

您可以使用 ZC7A62 css selector select <camera>不是<tonemapping>的子級。

選項#1 - 如果只有一個標簽:

soup.select_one(':not(tonemapping) > camera').extract()

選項#2 - 如果有多個標簽:

for cam in soup.select(':not(tonemapping) > camera'):
    cam.extract()

例子

from bs4 import BeautifulSoup
data="""
<scenedata>
   <tonemapping> <camera>...</camera> </tonemapping>
   <camera>...</camera>
</scenedata>
"""
soup=BeautifulSoup(data,"html.parser")

for cam in soup.select(':not(tonemapping) > camera'):
    cam.extract()

Output

<scenedata>
<tonemapping> <camera>...</camera> </tonemapping>

</scenedata>

我得到以下 output。 你可以試試這個。

from bs4 import BeautifulSoup

html_doc="""
<scenedata>
   <tonemapping> <camera>...</camera> </tonemapping>
   <camera>...</camera>
</scenedata>
"""

soup = BeautifulSoup(html_doc, 'html.parser')

s = soup.find('scenedata')
for t in s.select('camera'):
    t.decompose()

print(s)

Output

<scenedata>
<tonemapping>  </tonemapping>

</scenedata>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM