Answer the question
In order to leave comments, you need to log in
How to get HTML tags from a string in Python?
There is a task: to get only html tags from the text.
For example,
<html>
<head>
<title>Test</title>
</head>
<body class="body" style="color: red;">
<p id="1">Test</p>
<p id="2">Test</p>
</body>
</html>
<html><head><title></title></head><body><p></p><p></p></body></html>
Answer the question
In order to leave comments, you need to log in
I know ready methods built into python.
import re
s = """
<html>
<head>
<title>Test</title>
</head>
<body class="body" style="color: red;">
<p id="1">Test</p>
<p id="2">Test</p>
</body>
</html>"""
print(''.join(re.findall(r'</?[a-z]\w*\b|>', s, flags=re.I | re.M)))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question