Сохранить оцененный результат в переменную - кошмарный веб-парсинг - PullRequest
0 голосов
/ 06 августа 2020

Вот базовый код ... есть операторы require, которые я пропустил ... этот код выводит ссылку в консоли. Я хочу сохранить эту ссылку в переменной, чтобы использовать ее позже.

app.post('/search', function(req, res){
    const val = req.body.searchText;
    
    const nightmare = new Nightmare({ 
        show: true 
    });

    let aa;
    
    (async function() {
        await nightmare
    
        .goto('https://google.com/')
        .insert('#tsf > div:nth-child(2) > div.A8SBwf > div.RNNXgb > div > div.a4bIc > input', val + ' + MuzzMusic.com')
        .click('#tsf > div:nth-child(2) > div.A8SBwf > div.FPdoLc.tfB0Bf > center > input.gNO89b')
        .click('#rso > div:nth-child(1) > div > div.r > a > h3')


        .evaluate(() => {
            aa = document.querySelector('#post-54106 > div > div.entry-content > p:nth-child(4) > a').href
            return aa
        })

        .end()
        .then(console.log)
    
        .catch((err) => {
            console.log(err)
        })
        console.log('aa ==== ' + aa) //checking if it works
    
    })

1 Ответ

0 голосов
/ 06 августа 2020

Попробуйте следующее:

app.post('/search', async function(req, res){
    const val = req.body.searchText;
    
    const nightmare = new Nightmare({ 
        show: true 
    });

    let aa

    try {
        aa = await nightmare
            .goto('https://google.com/')
            .insert('#tsf > div:nth-child(2) > div.A8SBwf > div.RNNXgb > div > div.a4bIc > input', val + ' + MuzzMusic.com')
            .click('#tsf > div:nth-child(2) > div.A8SBwf > div.FPdoLc.tfB0Bf > center > input.gNO89b')
            .click('#rso > div:nth-child(1) > div > div.r > a > h3')
            .evaluate(() => document.querySelector('#post-54106 > div > div.entry-content > p:nth-child(4) > a').href)
            .end()
    } catch (error) {
        console.log(error)
    }
    
    console.log('aa ==== ' + aa) //checking if it works
})

...