@immelnikoff
Изучаю БД

Как с помощью python прочитать свойство fill-opacity в тэге path?

Собственно, на сайте есть есть данный элемент:
<svg width="45" height="68" viewBox="0 0 45 68" fill="none" xmlns="http://www.w3.org/2000/svg"><defs><linearGradient id="first"><stop offset="5%" stop-color="#F62727"></stop><stop offset="95%" stop-color="#F67227"></stop></linearGradient><linearGradient id="second"><stop offset="5%" stop-color="#F67227"></stop><stop offset="95%" stop-color="#FFDD29"></stop></linearGradient><linearGradient id="third"><stop offset="5%" stop-color="#FFDD29"></stop><stop offset="95%" stop-color="#13FF0E"></stop></linearGradient><linearGradient id="fourth"><stop offset="5%" stop-color="#13FF0E"></stop><stop offset="95%" stop-color="#31C1FF"></stop></linearGradient><linearGradient id="fifth"><stop offset="5%" stop-color="#31C1FF"></stop><stop offset="95%" stop-color="#2079FF"></stop></linearGradient><linearGradient id="sixth"><stop offset="5%" stop-color="#2079FF"></stop><stop offset="95%" stop-color="#2029FF"></stop></linearGradient></defs><line x1="0" y1="0" x2="0" y2="68" stroke="#333F48" stroke-opacity="0.25"></line><path class="shape" d="M0,68 H45 V48 L0,56" fill="url('#second')" fill-opacity="20%"></path></svg>


Как можно прочитать свойство fill-opacity в тэге path используя Python?
Результат должен быть: 20%.
  • Вопрос задан
  • 70 просмотров
Решения вопроса 1
Mike_Ro
@Mike_Ro Куратор тега Python
Python, JS, WordPress, SEO, Bots, Adversting
1. Заходим на сайт.
2. Ищем html тег по css селектору.
3. Достаем значение атрибута fill-opacity.
4. Печатаем значение атрибута fill-opacity.
import requests
from bs4 import BeautifulSoup

# go to website
url = 'https://example.com'
response = requests.get(url)

if response.status_code == 200:
    soup = BeautifulSoup(response.text, 'html.parser')
    path_tag = soup.find('path', {'class': 'shape'})  # CSS selector

    if path_tag is not None:
        fill_opacity = path_tag.get('fill-opacity', 'Ошибка: fill-opacity не обнаружен!')
    else:
        fill_opacity = 'Ошибка: fill-opacity не обнаружен!'
else:
    fill_opacity = f'Ошибка: не удалось подключиться к сайту {response.status_code}'

# result
print(fill_opacity)
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы