2013-02-25 4 views
0

내가 출력은 내가 말할 수 있도록 URL을Node.js를 크롤러는

<Buffer 3c 21 64 6f 63 74 79 70 65 20 68 74 6d 6c 3e 3c 68 74 6d 6c 20 69 74 65 
6d 73 63 6f 70 65 3d 22 69 74 65 6d 73 63 6f 70 65 22 20 69 74 65 6d 74 79 70 65 
...> 

변경 한 경우 https://github.com/sylvinus/node-crawler

var Crawler = require("crawler").Crawler; 

var c = new Crawler({ 
    "maxConnections":10, 
    "callback":function(error,result,$) { 
     console.log(result.body); 
    } 
}); 

c.queue("http://google.com"); 

에서 슈퍼 간단한 샘플 코드를 버퍼로 result.body을 가지고 있으며, HTML이 아닌 , c.queue("http://bing.com"), 나는 html 텍스트를 얻을 수 있습니다. 이 동작은 웹 사이트마다 다르며 이해할 수 없습니다.

console.log(result.body);에서 console.log($('body').text());으로 변경하면 google.com 사이트에서 내 브라우저로 전송 된 것과 다른 모양을 보이는 이상한 텍스트가 표시됩니다. 내가 좋아하는 Pinterest.com 다른 웹 사이트의 URL을 변경하는 경우

, 나는 jsdom.js 자체에 오류가 발생하기 시작했다.

C:\node_modules\crawler\node_modules\crawler\node_modules\jsdom\lib\jsdom.js:256 

     window.document.documentElement.appendChild(script); 
             ^
TypeError: Cannot call method 'appendChild' of null 
    at exports.env.exports.jsdom.env.processHTML (C:\node_modules\crawler\node_m 
odules\crawler\node_modules\jsdom\lib\jsdom.js:256:41) 
    at Array.forEach (native) 
    at exports.env.exports.jsdom.env.processHTML (C:\node_modules\crawler\node_m 
odules\crawler\node_modules\jsdom\lib\jsdom.js:239:18) 
    at Object.exports.env.exports.jsdom.env (C:\node_modules\crawler\node_module 
s\crawler\node_modules\jsdom\lib\jsdom.js:268:5) 
    at exports.Crawler.self.onContent.jsd (C:\node_modules\crawler\node_modules\ 
crawler\lib\crawler.js:243:37) 
    at exports.Crawler.self.onContent (C:\node_modules\crawler\node_modules\craw 
ler\lib\crawler.js:278:29) 
    at fs.readFile (fs.js:176:14) 
    at Object.oncomplete (fs.js:297:15) 

그래서 나는이 코드 자체에 약간의 오류가 생각하지만,하지 않을 경우, 누군가가 내 실수를 지적하는 데 도움이 될 수 있습니다?

참고 :jQueryUrl PARAM이 jQuery를 파일의 위치와 함께 작동하도록 얻을 new Crawler({})에 추가 할 수 있습니다.

답변

1

아직 보지 않았다면 Cheerio을보십시오. 그것은 당신에게보다 일관된 결과를 줄 것입니다. 나는 크롤러를 위해 그것을 잠시 뒤로 사용했고 당신이 설명하는 것과 같은 문제를 가지고 있지 않았다.

깨끗한 디자인으로 생각하기 때문에 선택합니다.

+0

2 일을 완전히 저장했습니다. –