2015-07-20 47 views
0

下面的代碼是我的Python代碼,用於從給定URL獲取指定值的美麗湯。BeautifulSoup - 從結果標籤中獲取價值

from bs4 import BeautifulSoup 
import urllib 
import re 

book = urllib.urlopen(url) 
bookpage = book.read() 
book.close()    
booksoup=BeautifulSoup(bookpage) 
bookTags=booksoup.findAll('div',{"class":"hotel_large_photp_score featured_review_score"}) 
print bookTags 

,當我打印bookTags這是我得到了(如下圖)

<div class="hotel_large_photp_score featured_review_score"> 
<a class="big_review_score_detailed js-big_review_score_detailed ind_rev_total hp_review_score" data-component="track" data-hash="cPWbOTCcdSdCBYSbfYCWXT" data-stage="1" data-tab-link="" data-track="click" href="#blockdisplay4" onclick="return false;" rel="reviews" style=""> 
<span data-component="track" data-hash="cPWbOTCcdSdCBYSbfYCWXT" data-stage="5" data-track="click"> 
<span class=" "> 
Very good 
</span> 
<span class="rating"> 
<span class="average">8.2</span><span class="out_of">/<span class="best">10</span></span> 
</span> 
</span> 
</a> 
<span class="trackit score_from_number_of_reviews"> 
Score from <strong class="count">229</strong> reviews 
</span> 

我需要的是從標籤<span class="average">8.2</span>價值8.2。請幫我拿到價值

回答

1

嘗試

average = bookTags.find("span", {"class": "average"}) 
print average.text 
+0

否Html文檔具有許多標籤。這就是爲什麼我檢索它的父標籤。 – Prem

+0

是的,但你只是在搜索父標籤 –

+0

'bookTag in bookTags: Rate = bookTag.find('span',{「class」:「average」}) print Rate.text' this for working。感謝你的付出 – Prem

0
bookTags=booksoup.findAll('div',{"class":"hotel_large_photp_score featured_review_score"}) 
for bookTag in bookTags:   
     Rate= bookTag.find('span',{"class":"average"}) 
     print Rate.text 

任何方式這爲我工作。感謝您的努力