How to get HTML tags from string in Python?

There is a task: to get only the html tags from the text.
For example,
<html> 
the <head>
<title>Test</title>
</head>
 <body class="body" style="color: red;">
 <p id="1">Test</p>
 <p id="2">Test</p>
</body>
</html>


The result should be
<html><head><title></title></head><body><p></p><p></p></body></html>

You also need to remove all attributes inside the tag. I wrote my solution, but it works for a long time. On one web page takes about 0.5 seconds. Maybe someone knows a ready built-in methods in bs4, selenium, or any other library?
March 19th 20 at 08:23
1 answer
March 19th 20 at 08:25
Solution
I know methods built into Python.
import re

s = """
<html> 
the <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)))

What are you doing here?

Find more questions by tags Beautiful SoupSeleniumParsingPython