BeautifulSoup - 从结果标签中获取价值
BeautifulSoup - getting value from the resultant tags
下面的代码是我的 python 代码,用于从给定的 URL.
中获取指定的值。
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
。请帮助我获得那个值
尝试
average = bookTags.find("span", {"class": "average"})
print average.text
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
这对我有用。谢谢你的努力
下面的代码是我的 python 代码,用于从给定的 URL.
中获取指定的值。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
。请帮助我获得那个值
尝试
average = bookTags.find("span", {"class": "average"})
print average.text
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
这对我有用。谢谢你的努力