删除除了某个标记及其内容之外的所有内容Python

问题描述:

我已经在互联网上搜索,找不到除了某个标记及其内容之外的所有内容。删除除了某个标记及其内容之外的所有内容Python

我该如何用Python(beautifulsoup 4)来做到这一点?

我有这个网站:

<p><iframe width="1000" height="500" allowfullscreen="allowfullscreen" class="embed" src="#"> </iframe></p> 
 
<p>sdkjasdkljasldjad;j dadas dasdadada</p>

我需要删除所有其他这样的输出是这样的:

<iframe width="1000" height="500" allowfullscreen="allowfullscreen" class="embed" src="#"> </iframe>

我来了了这一点,但它不知道该怎么走的更远:

@register.filter(name='only_iframe') 
def only_iframe(content): 
    soup = BeautifulSoup(content) 

    for tag in soup.find_all('p', 'strong'): 
     tag.replaceWith('') 

    return soup.get_text() 
+0

也许正则表达式会有所帮助,只是一个想法 – Robinlemon

为什么不找到iframe并获得其字符串表示

iframe = soup.find("iframe", class_="embed") 
print(str(iframe))