2017-03-21 8 views
0

これは、多くの「タグを削除しますがコンテンツを保持する」質問とは逆です - タグ内のテキストを削除するにはどうすればいいですか?(例:<span foo="bar">text I don't want</span>)(つまり<span foo="bar"></span>BeautifulSoup4のタグテキストを削除する

答えて

0

ちょうど空の文字列にtag.stringを設定する。また、tag.string.replace_with()を使用しています。

bs = BeautifulSoup(html) 
    for tag in bs.find_all('span'): 
     # Strip all text by setting it to an empty string 
     tag.string = "" 
     # Or strip all text by replacing the string 
     tag.string.replace_with("") 
関連する問題