2

我对我的任务有疑问。我用 GruntJs 写了一些应用程序。我必须通过 gruntJs 下载网页的源代码。

例如我有一个页面:example.com/index.html.

我想在 Grunt 任务中提供 URL,如下所示: scr: "example.com/index.html"

然后,我必须在文件中有这个源,ex: source.txt.

我怎样才能做到这一点?

4

1 回答 1

3

有几种方法可以解决这个问题。

http.get首先是评论中提到的来自 node.js API的原始文件。这将为您提供页面初始加载所提供的原始资源。当该站点在 ajax 请求之后广泛使用 javascript 来构建进一步的 html 时,就会出现问题。

第二种方法是使用实​​际的浏览器引擎来加载站点并在页面加载时执行任何 javascript 和进一步的 HTML 构建。最常见的引擎是PhantomJS,它封装在一个名为grunt-lib-phantomjs的 Grunt 库中。

幸运的是,有人在此基础上提供了另一层,几乎完全符合您的要求: https ://github.com/cburgdorf/grunt-html-snapshot

上面链接中的示例配置:

grunt.initConfig({
    htmlSnapshot: {
        all: {
          options: {
            //that's the path where the snapshots should be placed
            //it's empty by default which means they will go into the directory
            //where your Gruntfile.js is placed
            snapshotPath: 'snapshots/',
            //This should be either the base path to your index.html file
            //or your base URL. Currently the task does not use it's own
            //webserver. So if your site needs a webserver to be fully
            //functional configure it here.
            sitePath: 'http://localhost:8888/my-website/',
            //you can choose a prefix for your snapshots
            //by default it's 'snapshot_'
            fileNamePrefix: 'sp_',
            //by default the task waits 500ms before fetching the html.
            //this is to give the page enough time to to assemble itself.
            //if your page needs more time, tweak here.
            msWaitForPages: 1000,
            //if you would rather not keep the script tags in the html snapshots
            //set `removeScripts` to true. It's false by default
            removeScripts: true,
            //he goes the list of all urls that should be fetched
            urls: [
              '',
              '#!/en-gb/showcase'
            ]
          }
        }
    }
});
于 2013-07-04T11:49:04.807 回答