6

Ubuntu 12.04 でノード v0.10.11 を使用しています。URL のストリームを request モジュールで動作させるために何が欠けているのかわかりません。このプログラムは、メーリング リスト サイトにアクセスし、各月のダウンロード リンクを見つけて、各月のページをダウンロードしようとしています。

Mikael の readme には、「最初の引数は url または options オブジェクトのいずれかです。必要なオプションは URI のみで、他はすべてオプションです。uri || url - 完全修飾 uri または url.parse() から解析された url オブジェクト」と書かれています。

を呼び出すとurl.parse(www.targeturl.com)

Error: options.uri is a required argument

を使用しない場合はurl.parse

Error: Invalid 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);
4

1 に答える 1