見つかったすべての画像から「a」タグ (リンク) を削除したかったのです。したがって、パフォーマンスのために、html 内のすべての画像のリストを作成し、タグのラップを探して、リンクを削除するだけです。
私は BeautifulSoup を使用していますが、タグを削除する代わりに、内部のコンテンツを削除するのではなく、何が間違っているのかわかりません。
これは私がしたことです
from bs4 import BeautifulSoup
html = '''<div> <a href="http://somelink"><img src="http://imgsrc.jpg" /></a> <a href="http://somelink2"><img src="http://imgsrc2.jpg /></a>" '''
soup = BeautifulSoup(html)
for img in soup.find_all('img'):
print 'THIS IS THE BEGINING /////////////// '
#print img.find_parent('a').unwrap()
print img.parent.unwrap()
これにより、次の出力が得られます
> >> print img.parent()
<a href="http://somelink"><img src="http://imgsrc.jpg" /></a>
<a href="http://somelink2"><img src="http://imgsrc2.jpg /></a>
> >> print img.parent.unwrap()
<a href="http://somelink"></a>
<a href="http://somelink2"></a>
試してみましたが、またはを使用すると機能しませreplaceWith
んreplaceWithChildren
object.parent
findParent
何が間違っているのかわかりません。Pythonを始めてからわずか数週間です。