Answer the question
In order to leave comments, you need to log in
Why can't I pull the attribute?
const axios = require('axios')
const xpath = require("xpath-html")
const getWallData = async(domain) => {
try {
const respose = await axios.get(`https://m.vk.com/${domain}`)
const node = xpath
.fromPageSource(respose.data)
.findElements('//a[@class="wi_date"]')
console.log(node[0].toString())
} catch (error) {
console.error(error)
}
}
getWallData('club38463533')
<a class="wi_date" href="/wall-38463533_1314" rel="noopener" xmlns="http://www.w3.org/1999/xhtml">вчера в 14:53</a>
//a[@class="wi_date"]/@href
Error: Unexpected character :
at XPathParser.tokenize (/home/tony/Разработка/telegram-bot/node_modules/xpath-html/node_modules/xpath/xpath.js:1214:9)
at XPathParser.parse (/home/tony/Разработка/telegram-bot/node_modules/xpath-html/node_modules/xpath/xpath.js:1228:17)
at new XPathExpression (/home/tony/Разработка/telegram-bot/node_modules/xpath-html/node_modules/xpath/xpath.js:4287:17)
at Object.exports.selectWithResolver (/home/tony/Разработка/telegram-bot/node_modules/xpath-html/node_modules/xpath/xpath.js:4735:19)
at XPath.<anonymous> (/home/tony/Разработка/telegram-bot/node_modules/xpath-html/node_modules/xpath/xpath.js:4730:18)
at XPath.findElements (/home/tony/Разработка/telegram-bot/node_modules/xpath-html/src/XPath.js:48:24)
at getWallData (/home/tony/Разработка/telegram-bot/src/index.js:9:14)
at processTicksAndRejections (node:internal/process/task_queues:94:5)
Answer the question
In order to leave comments, you need to log in
findElements can only find nodes, to get an attribute there is .getAttribute("href"):
const node = xpath
.fromPageSource(respose.data)
.findElements('//a[@class="wi_date"]')
.getAttribute("href")
xpath-html
, but xpath
, but there will be a little more wrapper code
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question