回答編集履歴
1
HTML全体の中から、特定のtagのみ削除(その子供は残す)するコード
answer
CHANGED
|
@@ -16,4 +16,46 @@
|
|
|
16
16
|
#出力
|
|
17
17
|
<img class="hogehoge" src="http://hogehoge.com/fuga.jpg">
|
|
18
18
|
</img>
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
追記
|
|
22
|
+
---
|
|
23
|
+
HTML全体の中から、特定のtagのみ削除(その子供は残す)するコード。
|
|
24
|
+
```Python
|
|
25
|
+
import bs4
|
|
26
|
+
|
|
27
|
+
html = """
|
|
28
|
+
<body>
|
|
29
|
+
<p>
|
|
30
|
+
<a href="http://hogehoge.com/fuga.jpg">
|
|
31
|
+
<img class="hogehoge" src="http://hoge.example.com/hoge.jpg">
|
|
32
|
+
</a>
|
|
33
|
+
</p>
|
|
34
|
+
<p>
|
|
35
|
+
<a href="http://hogehoge.com/fuga.jpg">
|
|
36
|
+
<img class="hogehoge" src="http://fuga.example.com/fuga.jpg">
|
|
37
|
+
</a>
|
|
38
|
+
</p>
|
|
39
|
+
</body>
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
soup = bs4.BeautifulSoup(html, "html.parser")
|
|
43
|
+
|
|
44
|
+
while (soup.a):
|
|
45
|
+
soup.a.unwrap()
|
|
46
|
+
|
|
47
|
+
print(soup.prettify())
|
|
48
|
+
```
|
|
49
|
+
```
|
|
50
|
+
# 出力
|
|
51
|
+
<body>
|
|
52
|
+
<p>
|
|
53
|
+
<img class="hogehoge" src="http://hoge.example.com/hoge.jpg">
|
|
54
|
+
</img>
|
|
55
|
+
</p>
|
|
56
|
+
<p>
|
|
57
|
+
<img class="hogehoge" src="http://fuga.example.com/fuga.jpg">
|
|
58
|
+
</img>
|
|
59
|
+
</p>
|
|
60
|
+
</body>
|
|
19
61
|
```
|