![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
cheerio-httpcli
Advanced tools
Node.jsでWEBページのスクレイピングを行う際に必要となる文字コードの変換とHTMLのパースを行った後のオブジェクトを取得できるHTTPクライアントモジュールです。
WEBページの取得にはrequest、文字コードの変換にはiconv、HTMLのパースにはcheerioを使用しています。
cheerioはHTMLをjQueryライクにパースしてくれるモジュールです。パース後のオブジェクトを格納する変数名を「$」にすると、$('title').text()
のようなjQueryそのままの形で要素の情報を取得できます。
npm install cheerio-httpcli
url
で指定したWEBページをGETメソッドで取得し、文字コードの変換とHTMLパースを行いcallback
に返します。
callback
には以下の3つの引数が渡されます。
cheerio.load()
でHTMLコンテンツをパースしたオブジェクトresponse
オブジェクトGET時にパラメータを付加する場合は、get-param
に連想配列で指定します。
var client = require('cheerio-httpcli');
// Googleで「node.js」について検索する。
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'));
});
});
同梱の「example.js」はGoogle検索結果の一覧を取得するサンプルです。参考にしてください。
requestモジュールで使用するリクエストヘッダ情報の連想配列です。デフォルトではUser-Agent
のみIE9の情報を指定しています。
requestモジュールで指定するタイムアウト情報です。デフォルトでは30秒となっています。
サーバーとの通信にgzip転送を使用するかどうかを真偽値で指定します。デフォルトはtrue
(gzip転送する)です。
<head>
タグのcharset情報を参照しています。charsetで指定された文字コードとWEBページの実際の文字コードが異なる場合は変換エラーとなります。param
を追加fetch()
のcallbackの第3引数にrequestモジュールのresponse
オブジェクトを追加MIT licenseで配布します。
© 2013 ktty1220
FAQs
http client module with cheerio & iconv(-lite) & promise
We found that cheerio-httpcli demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.