2015-01-29 3 views
0

나는 npm install node-phantom에 의해 노드 팬텀를 설치했지만이 코드를 실행하고 할 때,이 오류를PhantomJS와 함께 node.js를 사용하여 스크래핑하는 방법?

var webpage = require('webpage').create(), 
    url = "https://www.example.com/cba/abc", 
    hrefs = new Array(); 
webpage.open(url,function(status){ 
    if(status=="success"){ 
     var results = page.evaluate(function(){ 
      $("#endpoints").each(function() { 
        hrefs.push($(this).attr("href")); 
      }); 
      return hrefs; 
     }); 
     console.log(JSON.stringify(results)); 
     phantom.exit(); 
    } 
}); 
+0

웹 페이지 모듈이 있는지 여부를 node_module 폴더에서 확인하십시오. –

답변

1

당신은 노드 팬텀의 웹 페이지 모듈을 필요로하지 않습니다를 Cannot find module 'webpage'주고있다. API를 사용하여 웹 페이지 모듈을 표시 할 수 있습니다. PhantomJS는 node.js와는 다른 실행 런타임을 가지므로이 방법으로 수행해야합니다. 그들은 일반적으로 동일한 모듈을 사용할 수 없습니다. 그렇기 때문에 node-phantomphantom 같은 두 실행 환경 사이에 다리가 있습니다. 그들은 본질적으로 PhantomJS의 API를 복제하여 node.js에서 사용합니다.

문서 당, 당신이 웹 페이지를 필요로하지 않는 대신 페이지를 얻을 :

var phantom = require('node-phantom'); 
phantom.create(function(err,ph) { 
    return ph.createPage(function(err,page) { 
    // do something with page: basically your script 
    }); 
}); 

당신은 그냥 복사하고 기존 PhantomJS 코드를 붙여 넣을 수 없습니다. 차이점이 있으므로 API (기본적으로 github의 README)를 연구해야합니다. 코드의

전체 번역 : 당신이 hrefs처럼 외부에서 변수를 사용할 수 없도록

var phantom = require('node-phantom'); 
phantom.create(function(err,ph) { 
    return ph.createPage(function(err,page) { 
    page.open(url,function(status){ 
     if(status=="success"){ 
     page.evaluate(function(){ 
      hrefs = []; 
      $("#endpoints").each(function() { 
      hrefs.push($(this).attr("href")); 
      }); 
      return hrefs; 
     }, function(err, results){ 
      console.log(JSON.stringify(results)); 
      ph.exit(); 
     }); 
     } 
    }); 
    }); 
}); 

page.evaluate은 여전히 ​​샌드 박스입니다.

관련 문제