2016-07-31 121 views
0

我已經在互聯網上搜索,找不到除了某個標記及其內容之外的所有內容。刪除除了某個標記及其內容之外的所有內容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

回答

0

爲什麼不找到iframe並獲得其字符串表示

iframe = soup.find("iframe", class_="embed") 
print(str(iframe)) 
相關問題