如何使用node.js从页面源中的此标记获取信息 - {{= flyingStatus(it.m_status)}}?

时间:2018-01-21 14:21:27

标签: javascript jquery node.js cloudflare cheerio

如果我查看页面源代码,我会看到“异常”属性值:

class = "bma-fly flying {{= flyingStatus(it.m_status) }}

通常对我来说这是:

class = "value"

加载页面后,如果我使用“Inspect”按钮,我会看到:

class = "bma-fly flying flying-won-team2 flying-past"

现在提问,如果我使用cheerio(jquery),如何使用来自这个“异常”属性值的Node.js获取信息,但什么也看不见??? 例如:

 request(link, function(err, resp, html) {
        if (!err){
          const $ = cheerio.load(html);
          let info = $("div.bma-fly.flying.flying-won-team2.flying-past");
          fs.writeFileSync("4.txt" , info); // nothing
        }
})

此外,在页面上使用 cloudflare的反ddos保护,可能这很重要。

1 个答案:

答案 0 :(得分:0)

所以,在页面上你有:

<div class = "bma-fly flying flying-won-team2 flying-past"> la la la </div>
<div class = "bma-fly flying flying-won-team2 flying-past"> kyrlik kyrkik </div>
<div class = "bma-fly flying flying-won-team2 flying-past"> bo bo bo </div>
<div class = "bma-fly flying flying-won-team2 flying-past"> info </div>
<div class = "bma-fly flying flying-won-team2 flying-past"> privet chelovek </div>

您想查看所有信息。 使用node.js和puppeteer的代码:

const puppeteer = require('puppeteer');
var fs = require('fs');

var link = "www. IIpuBeT Dpyr . com";

(async () => {
  console.log("Get info");
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto(link);
   });
   const text = await page.evaluate(() => {
      return [...document.body.querySelectorAll('.bma-fly.flying.flying-won-team2.flying-past')]
               .map(element => element.innerText)
               .join('\n');
    });

  console.log(text);
  fs.writeFileSync("nameFile.txt" , text);
  browser.close();
})();

更多信息:https://github.com/GoogleChrome/puppeteer/issues/1897