2015-10-05 47 views
2

我一直跑到牆壁,但感覺就像我在這裏。美麗的湯:解析只有一個元素

HTML塊正在收穫:

div class="details"> 
    <div class="price"> 
    <h3>From</h3> 
    <strike data-round="true" data-currency="USD" data-price="148.00" title="US$148 ">€136</strike> 
    <span data-round="true" data-currency="USD" data-price="136.00" title="US$136 ">€125</span> 
</div> 

我想解析出 「US $ 136」 單獨值(跨度的數據)。這裏是我的邏輯,到目前爲止,這同時捕捉「期間數據」和「罷工數據:

price = item.find_all("div", {"class": "price"}) 
     price_final = (price[0].text.strip()[8:]) 
     print(price_final) 

任何反饋表示讚賞:)

回答

1

price你的情況是ResultSet - 有名單的div標籤price類。現在,你需要找到的每個結果內span標籤(假設有要匹配多個價格):

prices = item.find_all("div", {"class": "price"}) 
for price in prices: 
    price_final = price.span.text.strip() 
    print(price_final) 

如果只有一次的價格,你需要找到:

soup.find("div", {"class": "price"}).span.get_text() 

或用CSS selector

soup.select_one("div.details div.price span").get_text() 

需要注意的是,如果你想用select_one(),安裝最新的beautifulsoup4包:

pip install --upgrade beautifulsoup4 
+0

太好了,謝謝您的反饋。現在工作 –