python - BeautifulSoup: get some tag from the page -


i have html-code

<div class="b-media-cont b-media-cont_relative" data-triggers-container="true"><span class="label">Двигатель:</span> бензин, 1.6 л<br/> <div class="b-triggers b-triggers_theme_dashed-buttons b-triggers_size_s b-triggers_text-notif"><div class="b-triggers__text">110 л.с.</div><div class="b-triggers__item b-triggers__item_notif" data-target="cost" data-target-container="[data-triggers-container]" data-toggle="tax_dropdown"><div class="b-link b-link_dashed">110 л.с.</div></div><div class="b-triggers-hidden-area b-triggers-hidden-area_width_240 b-triggers-hidden-area_close" data-target-bind="cost" style="left: 0px; top: 39px; width: 241px;">Налог на&nbsp;2016&nbsp;год <b>2&nbsp;750&nbsp;руб.</b><br/><br/><span class="gray">Расчет произведен на легковой автомобиль по <a href="http://law.drom.ru/calc/region77/skoda/rapid/2016/110/">калькулятору транспортного налога</a> для Москвы (<a href="http://www.drom.ru/my_region/">изменить регион</a>).</span></div></div><br/> <span class="label">Тип кузова:</span> хэтчбек<br/> <span class="label">Цвет:</span> золотистый<br/> <span class="label">Пробег:</span> <b>Новый автомобиль от официального дилера</b><br/> <span class="label">Руль:</span> левый<br/> <span class="label">vin:</span> xw8ac1nh7hk****32<br/> </div><p><span class="label">Данные по модели из каталога:</span>  <b>толян</b> <b>4 515 руб.</b> <b>Продажа Тойота Авенсис.</b> 

and need

<b>Новый автомобиль от официального дилера</b> 

i try

mileages = soup.find_all('span', class_='label').next_subling 

but returns attributeerror: 'resultset' object has no attribute 'next_subling' how can fix that?

attributeerror: 'resultset' object has no attribute 'next_subling'

this because find_all() returns multiple results - list of matching tags. and, problem covered beautifulsoup documentation:

attributeerror: 'resultset' object has no attribute 'foo' - happens because expected find_all() return single tag or string. find_all() returns list of tags , strings–a resultset object. need iterate on list , @ .foo of each one. or, if want 1 result, need use find() instead of find_all().

instead, should using find() locate specific label text , next sibling element:

mileages = soup.find('span', text=u'Пробег:').find_next_sibling("b").get_text(strip=true) 

this code works me is:

# -*- coding: utf-8 -*- bs4 import beautifulsoup  data = u""" <div class="b-media-cont b-media-cont_relative" data-triggers-container="true"><span class="label">Двигатель:</span> бензин, 1.6 л<br/> <div class="b-triggers b-triggers_theme_dashed-buttons b-triggers_size_s b-triggers_text-notif"><div class="b-triggers__text">110 л.с.</div><div class="b-triggers__item b-triggers__item_notif" data-target="cost" data-target-container="[data-triggers-container]" data-toggle="tax_dropdown"><div class="b-link b-link_dashed">110 л.с.</div></div><div class="b-triggers-hidden-area b-triggers-hidden-area_width_240 b-triggers-hidden-area_close" data-target-bind="cost" style="left: 0px; top: 39px; width: 241px;">Налог на&nbsp;2016&nbsp;год <b>2&nbsp;750&nbsp;руб.</b><br/><br/><span class="gray">Расчет произведен на легковой автомобиль по <a href="http://law.drom.ru/calc/region77/skoda/rapid/2016/110/">калькулятору транспортного налога</a> для Москвы (<a href="http://www.drom.ru/my_region/">изменить регион</a>).</span></div></div><br/> <span class="label">Тип кузова:</span> хэтчбек<br/> <span class="label">Цвет:</span> золотистый<br/> <span class="label">Пробег:</span> <b>Новый автомобиль от официального дилера</b><br/> <span class="label">Руль:</span> левый<br/> <span class="label">vin:</span> xw8ac1nh7hk****32<br/> </div><p><span class="label">Данные по модели из каталога:</span> <b>толян</b> <b>4 515 руб.</b> <b>Продажа Тойота Авенсис.</b> </div> """ soup = beautifulsoup(data, "html.parser")  mileages = soup.find('span', text=u'Пробег:').find_next_sibling("b").get_text(strip=true) print(mileages) 

prints:

Новый автомобиль от официального дилера 

Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -