简体   繁体   中英

BeautifulSoup: get tag text behind another tag

How to find tag by another tag using BeautifulSoup? In this example I want to get for example '0993 999 999' which is in div right behind another div with 'Telefon:' text.

I tried to get it using this:

print parsed.findAll('div',{'class':"dva" })[3].text 

But It does not work properly. I think there must be a way to tell BeautifulSoup that it is right behind 'Telefon' text or another way.

 <div class="kontakt">
        <h2 class="section-head">Kontaktné údaje</h2>
        <address itemprop="address"  itemscope itemtype="http://schema.org/PostalAddress" >
             <span itemprop="streetAddress" >SNP 12</span>, <span itemprop="postalCode" >904 01</span> <span itemprop="addressLocality" >Pezinok</span> </address>
        <div class="jedna">Telefon:</div>
        <div class="dva">013 / 688 27 78</div>
        <div class="jedna">Mobil:</div>
        <div class="dva">0993 999 999</div>
        <div class="jedna">Fax:</div
        <div class="dva">033 / 690 97 94</div>
        <div class="jedna">E-mail:</div>
        <div class="dva"><br /></div></div>

EDIT: I tried this, does not works neither.

tags = parsed.findAll('div',{'class':"jedna"})
for tag in tags:
    if tag.text=='Telefon:':
        print tag.next_siebling.string

Could you guys please give me a hint how to do that? Thanks!

You can use find_next_sibling() :

# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup

data = u"""html here"""

soup = BeautifulSoup(data)
print soup.find('div', text='Telefon:').find_next_sibling('div', class_='dva').text
print soup.find('div', text='Mobil:').find_next_sibling('div', class_='dva').text

Prints:

013 / 688 27 78
0993 999 999

FYI, you can extract the duplication and have a nice reusable function:

def get_field_value(soup, field):
    return soup.find('div', text=field+':').find_next_sibling('div', class_='dva').text

soup = BeautifulSoup(data)
print get_field_value(soup, 'Telefon')  # prints 013 / 688 27 78
print get_field_value(soup, 'Mobil')  # prints 0993 999 999

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM