代码之家  ›  专栏  ›  技术社区  ›  JSjohn

网页上的噩梦EJS输出

  •  0
  • JSjohn  · 技术社区  · 8 年前

    使用HTML CSS和JS。

    var Nightmare = require('nightmare');
    var nightmare = Nightmare({ show: false})
    
    nightmare
      .goto('https://en.wikipedia.org/wiki/Red_Hat_Enterprise_Linux')
    
      //.wait('#entry-content')
      .evaluate(function () {
        var ht = document.querySelector('#toc > ul').innerText;
        //return ht[0];
        //return (ht.split(/\r\n|\r|\n/).length);
        //check = document.querySelectorAll('#bodyblock > ul >li').length;
        //return check;
        //var ht1 = document.querySelectorAll('#bodyblock > ul > li ').innerText[5];
        //return ht1;
        return ht;
    
    
      })
      .end()
      .then(function (result) {
         console.log(result)
      })
      .catch(function (error) {
        return('Search failed:', error);
      });
    

    我试过: 例子:

    <script>
    function scrapedData(){
      document.getElementById("nighmareJSOutput").innerHTML = result;
    }
    </script>
    
    <body>
    
      <div>
        <p id="nighmareJSOutput"> </p>
        <button onclick="scrapedData()"> click me </button>
    
      </div>
    

    1 回复  |  直到 8 年前
        1
  •  1
  •   Dejan Toteff    8 年前

    我在评论示例中为您提供了所需的内容。如果你仍然面临问题,请提问。

    const express = require('express')
    const Nightmare = require("nightmare")
    const app = express()
    
    app.get('/', async function (req, res) {
      const nightmare = Nightmare({ show: false})
      const url = 'https://en.wikipedia.org/wiki/Red_Hat_Enterprise_Linux'
      await nightmare.goto(url)
      await nightmare.wait(2000)
      const result = await nightmare.evaluate(
        () => document.querySelector('#toc > ul').innerText
      )
      res.send(result)
    })
    
    app.listen(3000, function () {
      console.log('Example app listening on port 3000!')
    })