TransWikia.com

Как достать aria-label?

Stack Overflow на русском Asked by Rsfse on August 13, 2020

<div class="_5xu4"><i aria-label="New York, New York, profile picture" class="img _1-yc profpic" role="img" style="background:#d8dce6 url('https3a //scontent-arn2-1.xx.fbcdn.net/v/t1.30497-1/cp0/c14.0.48.48a/p48x48/83245568_1845797888897938_3274147281632231424_n.png?_nc_cat3d 126 _nc_sid3d 12b3be26 efg3d eyJpIjoidCJ926 _nc_ohc3d 4tVkeNlnOMMAX9P9kEF26 _nc_ht3d scontent-arn2-1.xx26 oh3d ab8d90eb6326f50989c103d1489c5bd726 oe3d 5F46AC94') no-repeat center;background-size:100% 100%;-webkit-background-size:100% 100%;width:48px;height:48px;"></i></div>
residence_city = soup.find('div',  '_5xu4')
residence_city = residence_city.findAll('aria-label')

One Answer

У вас маленькая ошибка, поскольку aria-label лежит в теге i, его bs может не увидеть. Также на сколько я знаю find() ищет теги, а не атрибуты. Вот рабочий код:

from bs4 import BeautifulSoup
html = '<div class="_5xu4"><i aria-label="New York, New York, profile picture" class="img _1-yc profpic" role="img" style="background:#d8dce6 url("https3a //scontent-arn2-1.xx.fbcdn.net/v/t1.30497-1/cp0/c14.0.48.48a/p48x48/83245568_1845797888897938_3274147281632231424_n.png?_nc_cat3d 126 _nc_sid3d 12b3be26 efg3d eyJpIjoidCJ926 _nc_ohc3d 4tVkeNlnOMMAX9P9kEF26 _nc_ht3d scontent-arn2-1.xx26 oh3d ab8d90eb6326f50989c103d1489c5bd726 oe3d 5F46AC94") no-repeat center;background-size:100% 100%;-webkit-background-size:100% 100%;width:48px;height:48px;"></i></div>'
soup = BeautifulSoup(html, 'lxml')
residence_city = soup.find('div',  '_5xu4')
residence_city = residence_city.find('i')
print(residence_city['aria-label'])

После исполнения кода Вы получите:New York, New York, profile picture

Correct answer by Evgeniy on August 13, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP