2012-07-31 74 views
1

我有以下的HTML,在这里我只想拿到产品名称而忽略html.How的其余部分,我可以做到这一点美丽的汤忽略内部HTML

我使用beautifulsoup Apple iPhone 4 Verizon

希望以此为oputpout
<h1 itemprop="itemreviewed">Apple iPhone 4 Verizon  
         <div class="right"> 
    <span class="s_button_follow_special" style="display: block"> 
    <a href="javascript:;" style="display: block" onclick="subscribe(this, 1, 5132);" class="follow_1_5132 s_button_2 s_button_follow" title="Follow Apple iPhone 4 Verizon"><em class="s_icon s_icon_follow"></em>Follow</a> 
    <a class="s_button_2 s_button_follow_arrow" href="javascript:;" onclick="subscribe(this, 1, 5132, '', 2);"></a> 
    </span> 
    <a href="javascript:;" style="display: none" onclick="subscribe(this, 1, 5132);" class="unfollow_1_5132 s_button_2 s_button_follow_disabled s_button_following" title="Unfollow Apple iPhone 4 Verizon"><span><em class="s_icon s_icon_following"></em>Following</span></a> 
    </div> 
    </h1> 


    header= soup('h1', {'itemprop' : 'itemreviewed'}) 
+0

我的例子 – Rajeev 2012-07-31 13:57:02

回答

0

Apple iPhone 4 Verizon文本解析树自己的元素,从任何其他独立;您可以通过获取附近的元素并使用nextSibling,previousSibling,nextprevious进行导航来选择它。

所以这应该工作:

header = soup.find('h1', itemprop='itemreviewed') 
text = header.next 
0

 
soup = BeautifulSoup(<h1 ....) 
header = soup.h1['itemprop'].contents

+1

年底给我想'.contents'将所有标签的内容,包括所有的HTML的获取,如DIV等等。你可以尝试使用'.contents [0]'来获得第一个元素。 – 2012-07-31 13:58:18

+0

你是对的,内容返回一个列表。 – Alexander 2012-08-01 06:31:11