npm осмос. Как я могу получить атрибут элемента?
часть HTML код страницы
<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 = " 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) Первый вопрос: как я могу извлечь содержимое attr со значением 3303 из meta itemprop="price"? ИЛИ с осмосом сделать нельзя?
2) Второй вопрос: почему я не могу получить значение 3 303 в этом <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);
Результат в cosole: test: ''
Может быть, проблема в JavaScript
сценарий и осмос не могут работать с этим?
2 ответа
В Cheerio это:
$('[itemprop="price"]').attr('content')
В осмосе? Понятия не имею, я никогда не слышал об этом.
Ваш селектор неверен.
Ответ на первый вопрос: селектор должен быть: 'meta[itemprop="price"]@content'
Вы можете сделать что-то вроде этого:
osmosis
.get('myURL.com')
.find('meta[itemprop="price"]@content')
.set('price')
.data(console.log) // {price : 3303}
Ответ на второй вопрос: правильный выбор
'div.detail-price-test > span#price_label'
'span#price_label'
'#price_label'
сделать что-то вроде:
osmosis
.get('myURL.com')
.find('div.detail-price-test > span#price_label')
.set('test')
.data(console.log); // {test : 3...}