2016-01-21 52 views
-1

我想解析下面的URL。我想獲得本網站上所有價格的輸出。第一個項目是59英鎊。如何搜索美麗的湯字符串

我檢查了元素,發現html看起來如下。我相信最好的方法是搜索一個類'sr_gs_rackrate_total',或者選擇一個以「Price for」開頭的標題。

如何在美麗的湯中做到這一點?

<strong class="price scarcity_color sr_gs_rackrate_price 
anim_rack_rate 
" title="Price for 1 night £59"> 
<b> 
<span class="sr_gs_rackrate_total">Total: </span> 
£59 
</b> 
</strong> 

http://www.booking.com/searchresults.en-gb.html?label=gen173nr-17CAEoggJCAlhYSDNiBW5vcmVmaFCIAQGYAS64AQTIAQTYAQHoAQH4AQs&sid=1a43e0952558ac0ad0061d5b6523a7bc&dcid=1&checkin_monthday=23;checkin_year_month=2016-1;checkout_monthday=24;checkout_year_month=2016-1;&city=-2601889&class_interval=1&csflt=%7B%7D&dtdisc=0&group_adults=7&group_children=0&highlighted_hotels=1192837&hlrd=0&hp_sbox=1&hyb_red=0&inac=0&label_click=undef&nflt=ht_id%3D201%3B&nha_red=0&no_rooms=1&redirected_from_city=0&redirected_from_landmark=0&redirected_from_region=0&review_score_group=empty&room1=A%2CA%2CA%2CA%2CA%2CA%2CA&sb_price_type=total&score_min=0&si=ai%2Cco%2Cci%2Cre%2Cdi&ss=London&ss_all=0&ssafas=1&ssb=empty&sshis=0&ssne=London&ssne_untouched=London&order=price_for_two

回答

1

下面是做到這一點的一種方法:

from bs4 import BeautifulSoup 
soup = BeautifulSoup(yourhtml) 
span = soup.find('span', {'class': 'sr_gs_rackrate_total'}) 
b = span.parent 
b.span.extract() 
b.text 

如果有一個以上的跨度,在這一個價格,使用

for span in soup.find_all('span', {'class': 'sr_gs_rackrate_total'}): 
    b = span.parent 
    b.span.extract() 
    print b.text 
+0

我是否需要放一個for循環? – Nickpick

+0

你在一個數組中有一堆物品。您需要一個for循環來逐個瀏覽這些項目並打印出它們的內容 – ytpillai