I created a simple web scraper with Node JS to extract watch prices, here is the code:
import axios from 'axios'
import cheerio from 'cheerio'
import * as fs from 'fs'
let Array1=[];
let a = 0;
let b=1;
axios.get("URL").then((response) => {
let $ = cheerio.load(response.data);
$(".article-title").each((i, e) => {
let title = $(e).text();
Array1[a] = title;
a = a 2;
}
)
$(".article-price").each((i, e) => {
let price = $(e).text();
Array1[b] = price;
b = b 2;
}
)
let data = JSON.stringify(Array1);
fs.writeFileSync('sub.json', data);
});
However, the JSON output is the following:
["\n Rolex Submariner 5513 Maxi MK5 Dial","\n \n \n 16 000 €\n ","\n Rolex Submariner Black Dial Date 41mm - 2021 - 126610LN","\n \n \n 14 839 €\n ","\n Rolex Black Dial Submariner Date 41mm - 2021 Card - 126610ln","\n \n \n 14 839 €\n ","\n Rolex Submariner Meter First MK1","\n \n \n 15 416 €\n ","\n Rolex Submariner 16610 military NATO 1994 S series","\n \n \n 35 000 €\n ","\n Rolex Submariner 126610LV Starbucks NEW 06/2021 Full set","\n \n \n 17 400 €\n ","\n Rolex 06/2021 - Submariner Date 126610 LN 99%new","\n \n \n 13 300 €\n ","\n Rolex Submariner 6536 Gilt James Bond 1956","\n \n \n 33 000 €\n ","\n Rolex Submariner Date Starbucks Automatik Edelstahl Herrenuhr...","\n \n \n 17 770 €\n ","\n Rolex Submariner","\n \n \n 12 500 €\n ","\n Rolex Submariner Date NOS avec stickers","\n \n \n 17 900 €\n ","\n Rolex Submariner 5513 \"Meter first\"","\n \n \n 16 000 €\n ","\n Rolex Submariner","\n \n \n 9 700 €\n ","\n Rolex Submariner 16610LV Kermit Y series 2003 Fat Four Mark 1...","\n \n \n 38 000 €\n ","\n Rolex Submariner Date 116610LN Boite/Papiers 2020","\n \n \n 13 790 €\n ","\n Rolex NEW 2021 some stickers - Submariner No Date 124060","\n \n \n 12 490 €\n ","\n Rolex Submariner 14060M Full Set","\n \n \n 10 900 €\n ","\n Rolex Submariner Date 18K Gold Automatik Herrenuhr Oyster...","\n \n \n 27 240 €\n ","\n Rolex \"Submariner \"\"Hulk\"\"\"Model 116610LV Full Set 2017","\n \n \n 19 950 €\n ","\n Rolex Submariner Date 116619LB Rolex Serviced 2017","\n \n \n 36 500 €\n ","\n Rolex Submariner 5513 Feet First","\n \n \n 16 000 €\n ","\n Rolex Submariner","\n \n \n 12 900 €\n ","\n Rolex Submariner 5513 Maxi dial 1979","\n \n \n 19 000 €\n ","\n Rolex Submariner Date 16610 Boite / Papiers","\n \n \n 11 750 €\n ","\n Rolex Mint 2021 KERMIT / STARBUCKS - Submariner Date 126610 LV...","\n \n \n 17 500 €\n ","\n Rolex Submariner 41 No-Date - 124060 - Full Set - 2021","\n \n \n 12 900 €\n ","\n Rolex Submariner Date Stahl / Gold Automatik Herrenuhr Oyster...","\n \n \n 9 550 €\n ","\n Rolex Submariner (No Date) 14060M 2 Lines Full set 2005","\n \n \n 11 200 €\n ","\n Rolex Submariner Date 41mm NEW 09/2021","\n \n \n 14 500 €\n ","\n Rolex Submariner 16610LV \"Kermit\"","\n \n \n 17 500 €\n ","\n Rolex Submariner Céramique 40mm","\n \n \n 12 900 €\n ","\n Rolex Submariner 5513 Gilt Glossy 1966 Rolex warranty","\n \n \n 48 000 €\n ","\n Rolex Submariner 14060M Boite / Papiers","\n \n \n 11 450 €\n ","\n Rolex Submariner date - Oyster Perpetual","\n \n \n 18 000 €\n ","\n Rolex Submariner Date - 116610LN- Full Set - 2012","\n \n \n 12 500 €\n ","\n Rolex Submariner No Date Automatik Stahl Oyster Perpetual Ref....","\n \n \n 10 650 €\n ","\n Rolex Submariner 116610 \"Hulk\" 2010 SERIE G FULL SET STICKERS","\n \n \n 22 500 €\n ","\n Rolex Submariner Date Or Blanc 41mm New 07/2021","\n \n \n 43 900 €\n ","\n Rolex Submariner Date 2000 Full set","\n \n \n 10 800 €\n ","\n Rolex Submariner","\n \n \n 11 900 €\n ","\n Rolex Submariner 16610 COMEX M series full set 2008","\n \n \n 89 000 €\n ","\n Rolex Submariner Date Purple dial","\n \n \n 12 290 €\n ","\n Rolex Submariner (No Date)","\n \n \n 11 000 €\n ","\n Rolex Submariner 5508 Tropical James Bond - 1958","\n \n \n 26 000 €\n ","\n Rolex Submariner Date Kermit Automatik Stahl Herrenuhr Ref....","\n \n \n 16 950 €\n ","\n Rolex montre rolex 5513 submariner 39 mm automatique en acier...","\n \n \n 11 120 €\n ","\n Rolex Submariner \"Kermit\"1661OLV","\n \n \n 17 000 €\n ","\n Rolex Submariner Date 1996 série W Full set","\n \n \n 10 900 €\n ","\n Rolex Submariner","\n \n \n 11 900 €\n ","\n Rolex Submariner 16610 S series 1994 tritium from Hawaii","\n \n \n 14 900 €\n ","\n Rolex Submariner Date Mk1 military NATO rare country code 905...","\n \n \n 13 290 €\n ","\n Rolex Submariner Date","\n \n \n 15 000 €\n ","\n Rolex Submariner 5513","\n \n \n 12 500 €\n ","\n Rolex Submariner Date Stahl / Gold Automatik Oyster Perpetual...","\n \n \n 14 250 €\n ","\n Rolex submariner 16613t 40 mm automatique or & acier gold new...","\n \n \n 12 845 €\n ","\n Rolex Submariner Date de 1988","\n \n \n 8 900 €\n ","\n Rolex Submariner No Date 14060 4 Lignes 2009 Full Set","\n \n \n 12 000 €\n ","\n Rolex Submariner \"Kermit\"","\n \n \n 17 000 €\n ","\n Rolex Submariner","\n \n \n 9 600 €\n ","\n Rolex Submariner Date 116613 LN","\n \n \n 13 500 €\n ","\n Rolex Submariner Date Blue Dial 40mm Gold steel FULL SET","\n \n \n 12 980 €\n ","\n Rolex Submariner No Date Stickers - 124060 - Full Set - 2020","\n \n \n 12 500 €\n ","\n Rolex Submariner Stahl / Gold Automatik Herrenuhr Ref....","\n \n \n 16 020 €\n ","\n Rolex Submariner Date","\n \n \n 11 150 €\n "]
How can I make this much more nice in order to fetch data and display in a HTML table using jQuery? I would like to a JSON output that looks like this:
[
{
"title": "Rolex Submariner 5513 Maxi MK5 Dial",
"price": "16 000"
},
{
"title": "Rolex Submariner Black Dial Date 41mm - 2021 - 126610LN",
"price": "14 839"
},
Many thanks!! Stuck here since days...
CodePudding user response:
Here is an example of how you can transform your JSON output to the format that you need:
function jsonToPrettyJson(json) {
const list = [];
for (const line of json) {
list.push(line.trim());
}
const prettyJson = [];
for (let i = 0; i < list.length; i = 2) {
prettyJson.push({ title: list[i], price: list[i 1] });
}
return prettyJson;
}
console.log(jsonToPrettyJson(json));
Output:
{
title: 'Rolex Submariner Stahl / Gold Automatik Herrenuhr Ref....',
price: '16 020 €'
},
{ title: 'Rolex Submariner Date', price: '11 150 €' }
...
CodePudding user response:
Array1[a] ={"title": $(e).text()}
array[b] = {"price": $(e).text()}
you should place key and their value in object and then place it in array. you put the value in title variable and then pass it to array so it replaced with their value not the word:"title" and so ...