Answer the question
In order to leave comments, you need to log in
Python: How to parse a link that is in a style?
Hello. There was a need for a case there:
There is a page of the site on which there are links to images, but they are in the style tag.
I provide an example:
<div class="UIMagicalImage_image RestaurantPageMenuItem_pictureImage" role="img" aria-label="Пример" style="background-image: url("https://primer.ru/test.jpeg");"></div>
Answer the question
In order to leave comments, you need to log in
I read a lot of different discussions like this, but I did not find an answer
from bs4 import BeautifulSoup
html = '''<html lang="en">
<body>
<div class="UIMagicalImage_image RestaurantPageMenuItem_pictureImage" role="img" aria-label="Пример" style="background-image: url("https://primer.ru/test.jpeg");"></div>
</body>
</html>
'''
soup = BeautifulSoup(html, 'html.parser')
div = soup.find('div')
print(div['style'])
import re
url = re.search(r'(https?://.+?)"', div['style']).group(1)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question