2014-02-28 40 views
0

請幫忙解決問題。如何選擇一個複雜的類?

HTML:

<form class="variants" action="/cart"> 
    <a class="thumb fancybox image_outer" href="products/apple-iphone-5s-16gb-black--space-gray-chernyj" data-fancybox-group="gallery5"> 
     <img src="http://first-store.ru/files/products/iphone%205S%20black_1.100x112.jpg?16ef5c4132fc88594851f92ccc2f3437" alt="Apple iPhone 5s 16GB Black &amp; Space Gray (Чёрный)" title="Apple iPhone 5s 16GB Black &amp; Space Gray (Чёрный)"> 
    </a> 

    <h1> 
     <a class="name_mark" data-product="1075" href="products/apple-iphone-5s-16gb-black--space-gray-chernyj">Apple iPhone 5s 16GB Black &amp; Space Gray (Чёрный)</a> 
    </h1> 

    <span class="price price_mark price_value">26 990&nbsp;<span class="currency">руб</span> 

     <input id="variants_2927" name="variant" value="2927" type="radio" class="variant_radiobutton" checked="" style="display:none;"> 

     <input class="button buy buy_button buy_button_catalog" type="submit" value="Купить" data-result-text="Добавлено"> 
    </span>  
</form> 

1代碼沒有奏效:

price = article.xpath('span[@class="price"]/span[@class="currency"]/text()')[0].strip() 
if price: 
    print(price) 

2代碼工作:

price = article.xpath('span/span[@class="currency"]/text()')[0].strip() 
if price: 
    print(price) 

,但我需要找到對模型#1 「價格」問題在於屬性類是由多個值組成的。

+0

[選擇CSS類XPath]中的可能重複(HTTP://計算器的.com /問題/ 8808921 /選擇-A-CSS-類與 - 的xpath) –

回答

2

[@class="price"]匹配只有當class屬性值正好是price

你需要類似的XPath如下:

price = article.xpath('span[contains(concat(" ", normalize-space(@class), " "), " price ")]/span[@class="currency"]/text()')[0].strip() 

也許你最好使用css selector

price = article.cssselect('span.price>span.currency')[0].text.strip() 
1

可以使用XPath像這樣的(只是選擇價格跨度):

article.xpath('span[contains(concat(" ", normalize-space(@class), " "), " price ")]') 

另一種可能性是使用CSS選擇:

article.cssselect('span.price')