A
A
Antosha Styazhkin2021-03-09 14:08:05
HTML
Antosha Styazhkin, 2021-03-09 14:08:05

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')


Conclusion:
<a class="wi_date" href="/wall-38463533_1314" rel="noopener" xmlns="http://www.w3.org/1999/xhtml">вчера в 14:53</a>


But if I do: //a[@class="wi_date"]/@href

Then immediately
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)


And I'm completely confused

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Anton, 2021-03-09
@desulaid

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")

or you need to use not xpath-html, but xpath, but there will be a little more wrapper code

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question