2013-07-04 1 views
6

우분투 12.04에서 노드 v0.10.11을 사용하고 있습니다. URL 스트림을 요청 모듈과 함께 작동 시키려면 무엇을 놓치고 있는지 알아낼 수 없습니다. 이 프로그램은 메일 링리스트 사이트로 이동하여 매월 다운로드 링크를 찾은 다음 매월 페이지를 다운로드하려고합니다.노드 요청을 던지고 있습니다 : 오류 : 잘못된 URI "www.urlworksinbrowser.com"또는 options.uri가 필수 인수입니다

mikeal의 readme는 "첫 번째 인수는 url이나 options 객체 일 수 있습니다. 유일한 필수 옵션은 uri이고 다른 모든 것은 선택적입니다. uri || url - 정규화 된 uri 또는 url의 구문 분석 된 url 객체입니다. parse() "

url.parse (www.targeturl.com)를 호출하면 [오류 : options.uri는 필수 인수입니다.] url.parse를 사용하지 않으면 [오류 : 잘못된 URI "www.freelists.org/archive/si-list/06-2013"] (이 링크는 브라우저에서 완벽하게 작동 함)

코드를 42 줄까지 줄였습니다. 어떤 조언을

var request = require('request'), 
    url = require('url'), 
    stream = require('stream'), 
    cheerio = require('cheerio'), // a reduced jQuery style DOM library 
    Transform = require('stream').Transform 

var DomStripStream = function(target) { 
    this.target = target; 
    stream.Transform.call(this,{objectMode: true}); 
} 
DomStripStream.prototype = Object.create(
    Transform.prototype, {constructor: {value: DomStripStream}} 
) 
DomStripStream.prototype.write = function() { 
    this._transform.apply(this, arguments); 
}; 
DomStripStream.prototype.end = function() { 
    this._transform.apply(this, arguments); 
    this.emit("end"); 
}; 

DomStripStream.prototype._transform = function(chunk, encoding, callback) { 
    chunk = chunk ? chunk.toString() : ""; 
    $ = cheerio.load(chunk); 
    domLinks = $(this.target); 
    $(domLinks).each(function (i, link) { 
    currLink = 'www.freelists.org' + $(link).attr('href') 
// currLink = url.parse(currLink) 
    request(currLink, function (error, response, body) { 
     console.log(error); 
    }) 
    }); 
} 

var fs = require("fs"), 
    output = fs.createWriteStream("out.txt"), 
    mainPage = new DomStripStream('td a') 

request('http://www.freelists.org/archive/si-list'). 
pipe(mainPage). 
pipe(output); 
+1

URL에하는 HTTP주세요 : // 또는 https : URL에 – user568109

답변

13

추가 HTTP를 환영합니다 : // 또는 https : //

+2

는 음, 어디, 우리가 어떻게의 루트를 보여 않는다 // URL? 경로를 알고 있지만 개발 또는 생산 중이라면 루트가 다르다 ... http : // {root} /path.... 나는 http : // 경로를 전달할 수 없다. .. –

+0

'document.location'을 검사하면'protocol' 필드가 포함됩니다. [MDN] (https://developer.mozilla.org/en-US/docs/Web/API/Document/location) – Laoujin