node.js - Node 请求抛出 : Error: Invalid URI "www.urlworksinbrowser.com" or options. uri 是必需的参数

标签 node.js parsing url request streams2

我在 Ubuntu 12.04 上使用 Node v0.10.11。我不知道我缺少什么来使 URL 流与请求模块一起工作。 该程序尝试转到邮件列表站点,找到每个月的下载链接,然后下载每个月的页面。

Mikael 的自述文件说“第一个参数可以是 url 或选项对象。唯一需要的选项是 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);

最佳答案

在url中添加http://或https://

关于node.js - Node 请求抛出 : Error: Invalid URI "www.urlworksinbrowser.com" or options. uri 是必需的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17476231/

相关文章:

javascript - 在 url 中使用 javascript "&"重定向

javascript - 表达 next() 头错误

javascript - 为什么 Gaze 消耗大量 CPU?

node.js - 安装 expo-cli : Could not resolve dependency:

android - Socket.io Android HTTPS - 连接错误 xhr 轮询错误

parsing - 解析器组合器 : how to terminate repetition on keyword

java - 我不知道如何实现递归语法分析器

c++ - 使用 C++ 从文件中解析坐标

ruby-on-rails - 如果不存在,请将 https) 添加到 URL?

cakephp - 如何在没有 ID 的情况下在 CakePHP 中使用 SEO url?