2017-05-22 1 views
0

特定の要素/クラスに囲まれた部分をhtmlを格納する変数から削除したいのですが、どうすればよいかわかりません。私はタグマッチングで要素を削除する方法

from urllib.parse import urlparse 

newcontent = content.find("div", {"class":"Footer"}).extract() 

以下のように実施する試み

例えば、次のHTML文書が変数に格納されている "コンテンツ"

<div class="content"> 
    <h1>content</h1> 
    <p>content<p> 
    <p>content</p> 
<div> 

<!-- want to delete from here --> 
<div class="Footer"> 
<div class=Footer-item> 
    ... 
    ... 

</div> 
</div> 

ただし、次のエラーが

TypeError: slice indices must be integers or None or have an __index__ method 
を発生しました

あなたは良い解決策があれば教えてください。

答えて

0

BeautifulSoupを使用してhtmlドキュメントを解析できます。

from bs4 import BeautifulSoup 

markup = "......<div class='footer'> ...</div>" 
soup = BeautifulSoup(markup,"html.parser") 
other_tags = soup 

soup.find('div',class_='footer').decompose() 

print (other_tags) 
関連する問題