BeautifulSoup:背后的另一个标记得到的标记文字 [英] BeautifulSoup: get tag text behind another tag
问题描述
如何找到使用BeautifulSoup另一个标记的标签?在这个例子中,我想获得例如0993 999 999,这是与背后的另一个div DIV权电话:文本。
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.
我试图把它用这个:
print parsed.findAll('div',{'class':"dva" })[3].text
但它不能正常工作。我认为必须有办法告诉BeautifulSoup,它是仅次于电话文本或另一种方式。
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>
编辑:我想这一点,不工作也不
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!
推荐答案
您可以使用<一个href=\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/#find-next-siblings-and-find-next-sibling\"相对=nofollow> find_next_sibling()
:
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
打印:
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
希望有所帮助。
这篇关于BeautifulSoup:背后的另一个标记得到的标记文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!