回答編集履歴
1
d
test
CHANGED
@@ -37,3 +37,75 @@
|
|
37
37
|
# ['HTML', 'BODY', 'DIV', 'p HELLO,WORLD./p', '/DIV', '/BODY', '/HTML']
|
38
38
|
|
39
39
|
```
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
## 追記
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
正規表現でもできなくはないですが、HTML パーサーのライブラリを使うと簡単にできます。
|
48
|
+
|
49
|
+
`pip install beautifulsoup4` でインストールできます。
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
```python
|
54
|
+
|
55
|
+
from bs4 import BeautifulSoup
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
apple = """<!DOCTYPE html>
|
60
|
+
|
61
|
+
<html>
|
62
|
+
|
63
|
+
<body>
|
64
|
+
|
65
|
+
<DIV>
|
66
|
+
|
67
|
+
<p>Hello, World.</p>
|
68
|
+
|
69
|
+
</DIV>
|
70
|
+
|
71
|
+
</body>
|
72
|
+
|
73
|
+
</html>
|
74
|
+
|
75
|
+
"""
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
soup = BeautifulSoup(apple)
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
html = soup.prettify()
|
86
|
+
|
87
|
+
html = html.replace("<!DOCTYPE html>", "") # <!DOCTYPE html> 削除
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
print(html)
|
92
|
+
|
93
|
+
# <html>
|
94
|
+
|
95
|
+
# <body>
|
96
|
+
|
97
|
+
# <div>
|
98
|
+
|
99
|
+
# <p>
|
100
|
+
|
101
|
+
# Hello, World.
|
102
|
+
|
103
|
+
# </p>
|
104
|
+
|
105
|
+
# </div>
|
106
|
+
|
107
|
+
# </body>
|
108
|
+
|
109
|
+
# </html>
|
110
|
+
|
111
|
+
```
|