首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用幻影爬虫打印html源代码到控制台

如何用幻影爬虫打印html源代码到控制台
EN

Stack Overflow用户
提问于 2016-03-27 14:10:13
回答 1查看 372关注 0票数 1

我刚刚下载并安装了nodejs的幻影爬虫。我将以下脚本复制并粘贴到一个名为crawler.js的文件中:

代码语言:javascript
复制
var Crawler = require('phantom-crawler');

// Can be initialized with optional options object 
var crawler = new Crawler();
// queue is an array of URLs to be crawled 
crawler.queue.push('https://google.com/');
// Can also do `crawler.fetch(url)` instead of pushing it and crawling it 
// Extract plainText out of each phantomjs page 
Promise.all(crawler.crawl())
.then(function(pages) {
  var texts = [];
  for (var i = 0; i < pages.length; i++) {
    var page = pages[i];
    // suffix Promise to return promises instead of callbacks 
    var text = page.getPromise('plainText');
    texts.push(text);
    text.then(function(p) {
      return function() {
        // Pages are like tabs, they should be closed 
        p.close()
      }
    }(page));
  }
  return Promise.all(texts);
})
.then(function(texts) {
  // texts = array of plaintext from the website bodies 
  // also supports ajax requests 
  console.log(texts);
})
.then(function () {
  // kill that phantomjs bridge 
  crawler.phantom.then(function (p) {
    p.exit();
  });
})

我想将完整的html源代码(在本例中是从google页面)打印到控制台。

我搜索了很多次,但是我没有发现类似的东西,所以我该怎么做呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-27 16:05:22

获取content而不是plainText承诺。

模块幻影爬虫使用模块节点-幻影-简单,该模块使用幻影

您可以在幻影维基中找到可以调用的属性列表。

代码语言:javascript
复制
var Crawler = require('phantom-crawler');

// Can be initialized with optional options object
var crawler = new Crawler();
// queue is an array of URLs to be crawled
crawler.queue.push('https://google.com/');
// Can also do `crawler.fetch(url)` instead of pushing it and crawling it
// Extract plainText out of each phantomjs page
Promise.all(crawler.crawl())
.then(function(pages) {
  var allHtml = [];
  for (var i = 0; i < pages.length; i++) {
    var page = pages[i];
    // suffix Promise to return promises instead of callbacks
    var html = page.getPromise('content');
    allHtml.push(html);
    html.then(function(p) {
      return function() {
        // Pages are like tabs, they should be closed
        p.close()
      }
    }(page));
  }
  return Promise.all(allHtml);
})
.then(function(allHtml) {
  // allHtml = array of plaintext from the website bodies
  // also supports ajax requests
  console.log(allHtml);
})
.then(function () {
  // kill that phantomjs bridge
  crawler.phantom.then(function (p) {
    p.exit();
  });
})
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36248275

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档