0
私はJavascript(Express)で個人用にウェブサイトスクレーパーを構築しています。ループ内の偶数個のオブジェクトから配列を作成する
このスクリプトの目的は、外部ソースからいくつかの単純なテキストデータをスクラップし、最終的にスクラップしたデータをJSON
オブジェクトとして出力することです。 しかし、常に均等な量のオブジェクトが存在するので、私はそれらをペアとして配列に追加したいと思います。これはあなたからの指導が必要な部分です。
電流出力がちょうど一般的なJSONオブジェクト/オブジェクト掻き取ったオブジェクト(通常は8-16の間)の量に基づいています。
{
name: "John Doe",
email: "[email protected],
status: "active"
},
{
name: "Jane Doe",
email: "[email protected],
status: "inactive"
},
{
name: "Johnny Walker",
email: "[email protected]",
status: "active"
},
{
name: "Jimmy Glenfiddich",
email: "[email protected]
status: "active"
}
と期待される出力は次のようなものになります。
を{
"pair-number": 1,
"pair:" [
{
name: "John Doe",
email: "[email protected],
status: "active"
},
{
name: "Jane Doe",
email: "[email protected],
status: "inactive"
},
]
},
{
"pair-number": 2,
"pair:" [
{
name: "Johnny Walker",
email: "[email protected]",
status: "active"
},
{
name: "Jimmy Glenfiddich",
email: "[email protected]
status: "active"
}
]
}
ここに私のserver.js
var express = require('express');
var request = require('request');
var cheerio = require('cheerio');
var app = express();
var url = 'http://testurl.com;
var name, email, status;
app.get('/scrape', function(req, res) {
request(url, function (error, response, html) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
data = {"name": name, "email": email, "status": status };
$('.scrape-class').filter(function() {
var that = $(this);
name = that.find('h5').text();
email = that.find('.email').text();
status = that.find('dl').children().first().text();
data.name = name;
data.email = email;
data.status = status;
console.log(data);
});
}
});
});
app.listen(80, function() {
console.log('Example app listening on port 80!')
})