BeautifulSoup - 从结果标签中获取价值

时间:2015-07-20 10:45:46

标签: python beautifulsoup

以下代码是我的python代码,其中包含用于从给定URL获取指定值的优美内容。

Close

当我打印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> 中的值8.2。请帮助我获得这个价值

2 个答案:

答案 0 :(得分:1)

尝试

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

答案 1 :(得分: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

这对我有用。谢谢你的努力