How with use node.js get information from this tag

2019-08-29 05:01发布

If I look page source, I see "unusual" attribute value:

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

Usually for me this is:

class = "value"

After loaded page, if I use button "Inspect", I see:

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

Now question, how get information with use Node.js from this "unusual" attribute value, if I use cheerio (jquery), but see nothing??? For example:

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

Moreover, on page use cloudflare's anti-ddos protection, may be this is important.

1条回答
手持菜刀,她持情操
2楼-- · 2019-08-29 05:42

So, on page you have:

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

And you want see all information. Code for that with use node.js and 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();
})();

more informationt there: https://github.com/GoogleChrome/puppeteer/issues/1897

查看更多
登录 后发表回答