【发布时间】:2018-08-14 18:10:20
【问题描述】:
我有一个非常简单的文件,它应该从一个网页中抓取一些数据。在阅读之后,我将自己设置为 artoo、request 和cheerio,但现在我被卡住了。这是我目前的代码。
request('http://www.ciclopi.eu/frmLeStazioni.aspx?ID=144', function (error, response, html) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
artoo.bootstrap(cheerio);
var scraper = $('span.Stazione, span.TableComune, span.Red').scrape({
class: 'class',
content: 'text'
});
console.log(scraper);
}
});
此代码解析为 scraper 变量作为结构如下的 json 对象:
[ { class: 'Stazione', content: 'Galleria Gerace' },
{ class: 'TableComune', content: 'Via Carlo Matteucci' },
{ class: 'Red', content: '7 bici libere5 posti disponibili' },
{ class: 'Stazione', content: 'C. Marchesi' },
{ class: 'TableComune', content: 'Via M. Valgimigli' },
{ class: 'Red', content: '2 bici libere10 posti disponibili' },
{ class: 'Stazione', content: 'CNR-Praticelli' },
{ class: 'TableComune', content: 'Via G. Moruzzi' },
{ class: 'Red', content: '7 bici libere7 posti disponibili' } ]
我需要将 scraper 对象重新排列成这样的
scraper = [
{
"Name": the content for class Stazione,
"Address": the content for class TableComune,
"Bikes": the content for class Red
},
{
"Name": the content for class Stazione,
"Address": the content for class TableComune,
"Bikes": the content for class Red
}
...
]
我真的一头雾水,希望我能解释一下自己..
【问题讨论】:
-
myObj在你的帖子中是完全无效的实体,你能更准确地指定你想要的结果吗? -
这就是我希望我的对象最终的样子,它基本上将刮板对象中的“Stazione”、“TableComune”和“Red”分组。它们是自行车共享站的数据。我会尝试以更好的方式进行编辑。
-
您的更新有所帮助,因此我能够解决您的问题。
标签: javascript arrays json sorting data-structures