Extract content of style tags

Extract content of style tags
0

I want to extract the content of all the HTML style tags using regular expression but the content doesn’t display.

import re
html = '<style type="text/css">h1{color:blue;}p{color:red;}</style><h1>Hi</h1><p>test <span class="time">test</span></p>'
tags = re.findall(r'<[^>]+>', html)
for a in tags:
    content=re.findall(r'<style>(.*?)</style>',a)
    print(content)

Hello! I never used regular expressions (I’m still a newbie in Python) but I share a possible solution that came to my mind, it’s simple but it solves your problem.

html = """<style type="text/css">
h1{color:blue;}
p{color:red;}
span{color:white}
</style>
<h1>Hi</h1><p>test <span class="time">test</span></p>"""

open_tag = '<style type = "text/css">'
close_tag = '</style>'

start = html.find(open_tag) + len(open_tag)
end = html.find(close_tag)

print('result:',html[start:end], sep = '\n')

result:
h1{color:blue;}
p{color:red;}
span{color:white}