John Laybues
John Laybues

Reputation: 113

npm osmosis. How i can get attr of element?

part HTML code of page

<div name="price" class="detail-price-test">
  <meta itemprop="price" content="3303">
  <meta itemprop="priceCurrency" content="test">
  <span id="price_label">3 303</span><span class="detail-price-test-sign" id="price_label_sign"> eur</span>
  <script>
    if (price_json.price != '0') {
    var price_container = document.getElementById('price_container'),
    price_cheaper_selector = 'detail-price-cheaper';
    document.getElementById('price_label').innerHTML = price_json.price_formatted;
    document.getElementById('price_label_sign').innerHTML = "&thinsp;eur";
    if (parseFloat(price_json.old_price) >
    parseFloat(price_json.price) &&
    price_container &&
    !price_container.hasClass(price_cheaper_selector)
    ) {
    price_container.addClass(price_cheaper_selector);
    }
    }
  </script>
  <link itemprop="availability" href="http://schema.org/InStock">
</div>

1) First question: How i can extract attr content with value 3303 from meta itemprop="price" ? OR with osmosis, it is impossible to make?

2) Second question: Why i cant get value 3 303 in this <span id="price_label">3 303</span>

osmosis
.get('myURL.com')
.find('div.detail-price-test span#price_label') //or div.detail-price-test span[id=price_label]
.set('test')
.data(console.log);

Result in cosole: test: ''

Maybe the problem in JavaScript script and osmosis can't work with this?

Upvotes: 2

Views: 778

Answers (2)

Sachi
Sachi

Reputation: 1386

Your selector is incorrect.

First question answer: The selector should be: 'meta[itemprop="price"]@content' You can do something like this:

osmosis
.get('myURL.com')
.find('meta[itemprop="price"]@content')
.set('price')
.data(console.log) // {price : 3303}

Second question answer: The correct selector is either

  1. 'div.detail-price-test > span#price_label'
  2. 'span#price_label'
  3. '#price_label'

do something like:

osmosis
.get('myURL.com')
.find('div.detail-price-test > span#price_label')
.set('test')
.data(console.log); // {test : 3...}

Upvotes: 0

pguardiario
pguardiario

Reputation: 55002

In Cheerio it's:

$('[itemprop="price"]').attr('content')

In osmosis? No idea, I've never heard of it.

Upvotes: 0

Related Questions