0

please help to solve the problem.

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 code not worked:

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

2 code worked:

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

but I need to find a "price" on the model # 1. the problem is that the attribute class is composed of several values.

Sergey
  • 869
  • 2
  • 13
  • 22
  • possible duplicate of [Selecting a css class with xpath](http://stackoverflow.com/questions/8808921/selecting-a-css-class-with-xpath) – Jens Erat Feb 28 '14 at 19:15

2 Answers2

2

[@class="price"] matches only when the class attribute value is exactly price.

You need xpath similar to following:

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

Maybe you'd better to use css selector:

price = article.cssselect('span.price>span.currency')[0].text.strip()
falsetru
  • 357,413
  • 63
  • 732
  • 636
1

You can use an xpath like this one(just selects the price span):

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

Another possibility is using a css selector:

article.cssselect('span.price')
stranac
  • 26,638
  • 5
  • 25
  • 30