New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

cheerio-httpcli

Package Overview
Dependencies
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cheerio-httpcli - npm Package Compare versions

Comparing version 0.1.1 to 0.1.2

test/encoding.test.js

2

example.js

@@ -8,3 +8,3 @@ #!/usr/bin/env node

// google-search
client.fetch('http://www.google.com/search', { q: 'node.js' }, function (err, $) {
client.fetch('http://www.google.com/search', { q: 'node.js' }, function (err, $, res) {
if (err) {

@@ -11,0 +11,0 @@ return console.error(err);

@@ -5,2 +5,4 @@ /*jshint node:true, forin:false */

var request = require('request'),
urlParser = require('url'),
zlib = require('zlib'),
cheerio = require('cheerio');

@@ -52,2 +54,41 @@ try {

/**
* request with gzip-transfer
* @param param request parameter for request method
* @param gzip true: use gzip-transfer
* @param callback (err, response, body(buffer))
*/
function _requestEx(param, gzip, callback) {
if (gzip) {
// add gzip-header
param.headers['Accept-Encoding'] = 'gzip, deflate';
}
// do request
request(param, function (err, res, body) {
if (err) {
return callback(err, res, body);
}
// find gzip-encoding in response header
var gzipped = false;
for (var h in res.headers) {
if (h.toLowerCase() === 'content-encoding' && res.headers[h].toLowerCase() === 'gzip') {
gzipped = true;
break;
}
}
if (gzipped) {
// unzip response body
zlib.unzip(body, function (err, buf) {
callback(err, res, buf);
});
} else {
// do nothing
callback(undefined, res, body);
}
});
}
/**
* module core

@@ -64,2 +105,5 @@ */

// use gzip-transfer
gzip: true,
/**

@@ -70,3 +114,3 @@ * fetch html, and convert html-encoding to utf8, and parse html using cheerio

* @param encode request-html-encoding (default: auto guess)
* @param callback (err, parsed html-contents)
* @param callback (err, parsed html-contents, response)
*/

@@ -84,18 +128,23 @@ fetch: function (url, param, encode, callback) {

// convert html-encoding
request({
// do request
var parsed = urlParser.parse(url);
var reqHeader = { Host: parsed.host };
for (var h in this.headers) {
reqHeader[h] = this.headers[h];
}
_requestEx({
uri: url,
encoding: null,
headers: this.headers,
encoding: null, // stop auto-encoding
headers: reqHeader,
timeout: this.timeout,
qs: param
}, function (err, res, body) {
}, this.gzip, function (err, res, body) {
if (err) {
return callback(_fetchError(err, { url: url }));
return callback(_fetchError(err, { url: url }), undefined, res);
}
if (res.statusCode !== 200) {
return callback(_fetchError('server status', { statusCode: res.statusCode, url: url }));
}
if (!body) {
return callback(_fetchError('body is undefined', { statusCode: res.statusCode, url: url }));
return callback(_fetchError('body is undefined', {
statusCode: res.statusCode,
url: url
}), undefined, res);
}

@@ -113,3 +162,3 @@

} catch (e) {
return callback(_fetchError(e, { charset: enc, url: url }));
return callback(_fetchError(e, { charset: enc, url: url }), res);
}

@@ -119,5 +168,14 @@ }

return callback(err, cheerio.load(body));
// check http status code
var errInfo = undefined;
if (res.statusCode !== 200) {
errInfo = _fetchError('server status', {
statusCode: res.statusCode,
url: url
});
}
return callback(errInfo, cheerio.load(body), res);
});
}
};
{
"name": "cheerio-httpcli",
"version": "0.1.1",
"version": "0.1.2",
"description": "html client module with cheerio & iconv",
"main": "index.js",
"scripts": {
"test": "vows --spec test/test.js"
"test": "vows --spec"
},

@@ -9,0 +9,0 @@ "dependencies": {

@@ -1,3 +0,5 @@

# cheerio-httpcli - iconvによる文字コード変換とcheerioによるHTMLパースを組み込んだNode.js用HTTPクライアントモジュール
# cheerio-httpcli
#### iconvによる文字コード変換とcheerioによるHTMLパースを組み込んだNode.js用HTTPクライアントモジュール
Node.jsでWEBページのスクレイピングを行う際に必要となる文字コードの変換とHTMLのパースを行った後のオブジェクトを取得できるHTTPクライアントモジュールです。

@@ -17,6 +19,10 @@

`url`で指定したWEBページをGETメソッドで取得し、文字コードの変換とHTMLパースを行いcallbackに返します。
`url`で指定したWEBページをGETメソッドで取得し、文字コードの変換とHTMLパースを行い`callback`に返します。
`callback`の引数は、(Errorオブジェクト, `cheerio.load()`の戻り値)です。
`callback`には以下の3つの引数が渡されます。
* Errorオブジェクト
* `cheerio.load()`でHTMLコンテンツをパースしたオブジェクト
* requestモジュールの`response`オブジェクト
GET時にパラメータを付加する場合は、`get-param`に連想配列で指定します。

@@ -29,11 +35,16 @@

// Googleで「node.js」について検索する。
client.fetch('http://www.google.com/search', { q: 'node.js' }, function (err, $) {
client.fetch('http://www.google.com/search', { q: 'node.js' }, function (err, $, res) {
// レスポンスヘッダを参照
console.log(res.headers);
// HTMLタイトルを表示
console.log($('title').text());
// リンク一覧を表示
$('a').each(function (idx) {
console.log($(this).attr('href'));
});
});
Google検索結果の一覧を取得する方法は「example.js」を参照してください。
同梱の「example.js」はGoogle検索結果の一覧を取得するサンプルです。参考にしてください。

@@ -44,8 +55,12 @@ ## プロパティ

`request`で使用するリクエストヘッダ情報の連想配列です。デフォルトでは`User-Agent`のみIE9の情報を指定しています。
requestモジュールで使用するリクエストヘッダ情報の連想配列です。デフォルトでは`User-Agent`のみIE9の情報を指定しています。
### timeout
`request`で指定するタイムアウト情報です。デフォルトでは30秒となっています。
requestモジュールで指定するタイムアウト情報です。デフォルトでは30秒となっています。
### gzip
サーバーとの通信にgzip転送を使用するかどうかを真偽値で指定します。デフォルトは`true`(gzip転送する)です。
## その他

@@ -58,2 +73,9 @@

### 0.1.2 (2013-09-06)
* リクエストヘッダのHostを自動でセットするようにした
* gzip転送オプション追加
* `fetch()`のcallbackの第3引数にrequestモジュールの`response`オブジェクトを追加
* HTTPステータスコードが200以外によるエラーでもコンテンツを取得するようにした
### 0.1.1 (2013-04-11)

@@ -60,0 +82,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc