Я строю простой веб-скребок на Node.js. Я также использую Express.js, Request и Cheerio. Я пытаюсь скрести по следующему адресу: http://www.houzz.com/professionals/c/Nashville,-TNКак преобразовать JSON из одного формата в другой?
var express = require('express');
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
var app = express();
app.get('/scrape', function(req, res){
url = 'http://www.houzz.com/professionals/c/Nashville,-TN';
request(url, function(error, response, html){
//console.log(html)
if(!error){
var $ = cheerio.load(html);
var title = [], contact = [], review = [], description = [];
var json = { title : "", contact : "", review: "", description: ""};
$('.pro-title').filter(function(){
var data = $(this);
title.push(data.text());
console.log(title);
json.title = title;
});
$('.pro-phone').filter(function(){
var data = $(this);
contact.push(data.text());
console.log(contact);
json.contact = contact;
});
$('.pro-review-string').filter(function(){
var data = $(this);
review.push(data.children().first().text());
json.review = review;
});
$('.pro-description').filter(function(){
var data = $(this);
description.push(data.text());
json.description = description;
});
}
fs.writeFile('houzz.json', JSON.stringify(json, null, 4), function(err){
console.log('File successfully written! - Check your project directory for the houzz.json file');
});
res.send('Check your console!');
}) ;
});
app.listen('8081');
console.log('Port 8081');
exports = module.exports = app;
Образец выдаваемого houzz.json
файл выглядит следующим образом:
{
"title": [
"Marcelle Guilbeau, Interior Designer",
"Country Flooring DIrect",
"Eric Ross Interiors, LLC",
"Hermitage Kitchen Design Gallery",
"William Johnson Architect",
],
"contact": [
"(615) 815-9309",
"(615) 646-0366",
"(615) 472-8236",
"(615) 843-3310",
"(615) 292-4017",
],
"review": [
"77",
"1",
"14",
"14",
"15",
],
"description": [
"Marcelle takes her clients on a journey, drawing out their needs to create an oasis that reflects their personal sense of style and renews their connection to those things about...\t\t\tRead More\n\t\t\t",
"Country Flooring Direct is the local flooring option that will handle your flooring needs. Give Country Flooring Direct a call and find out why lower overhead means lower prices.\t\t\tSee my projects\n\t\t",
"Eric Ross Interiors exists to create beautiful interiors and a luxury design experience for its clients. We are committed to creating whole room environments for our clients in...\t\t\tRead More\n\t\t\t",
"We are a total design center that offers the finest in custom cabinetry, with the best possible level of creativity, design and service. We are located within Hermitage Lighting Gallery.\t\t\tSee my projects\n\t\t",
"William C. Johnson Architect, LLC is a small, full service architectural design firm. Since 1985, WCJA has helped clients achieve their design goals, from small residential...\t\t\tRead More\n\t\t\t",
]
}
Как реструктурировать файл houzz.json
и сделать его выглядеть следующим образом:
{
0:
[
title:
contact:
review:
description:
1:
[
title:
contact:
review:
description:
]
Ваш желаемый результат плохо отформатирован JSON. Вы не должны использовать числа как имена свойств, и вы не можете иметь массив свойств. – krillgar
Знаете ли вы, как это исправить? Вероятно, мне нужно запросить элементы DOM в цикле. –
Следует отметить, что термины для этого сайта включают: «Вам прямо запрещено использование интеллектуального анализа данных, роботов или подобных инструментов сбора и извлечения данных при использовании Сайта». –