M
M
mffff42021-06-22 17:42:48
Python
mffff4, 2021-06-22 17:42:48

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(&quot;https://primer.ru/test.jpeg&quot;);"></div>


The answer that I would like to receive:
https://primer.ru/test.jpeg

How can this be implemented. I read a lot of different similar discussions, but did not find an answer.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-06-22
@SoreMix

I read a lot of different discussions like this, but I did not find an answer

Attributes of all tags can be obtained simply by the key, as with a dictionary.
from bs4 import BeautifulSoup

html = '''<html lang="en">
<body>
    <div class="UIMagicalImage_image RestaurantPageMenuItem_pictureImage" role="img" aria-label="Пример" style="background-image: url(&quot;https://primer.ru/test.jpeg&quot;);"></div>
</body>
</html>
'''

soup = BeautifulSoup(html, 'html.parser')
div = soup.find('div')
print(div['style'])

Further, as you please, at least through find, at least through regex.
import re
url = re.search(r'(https?://.+?)"', div['style']).group(1)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question