I'm trying to scrape the data from a website using axios and cheerio in nodejs but the problem is I couldn't get the desired value as the html is different here, The HTML is given below
<div id="cars"> <new-chart-modal :template-widgets-json='[{id: 1, Value: "10000"},{id: 2, Value: "20000"}]' @close-widget-modal="closeWidgetModal" @loaded-widget-template="loadedWidgetTemplate" ref="newWidgetModal" > </new-chart-modal> <dashboard-grid :widgets-json='[{id: 1, name: "Volvo"}, {id: 2, name: "BMW"}]' @edit-in-tab="editWidgetInTab" ref="grid" > </dashboard-grid> </div>
In the above html, I need to get the value of :widgets-json inside dashboard-grid tag alone, I'm unable to do it. I looking for some answers or tips to get the value of :widgets-json inside the which is [{id: 1, name: "Volvo"}, {id: 2, name: "BMW"}]. can somebody please help me out? Thanks in advance.
CodePudding user response:
like this:
const axios = require('axios');
const cheerio = require('cheerio');
async function scrape() {
// make a request to your target website
const response = await axios.get('http://example.com');
const $ = cheerio.load(response.data);
const dashboardGrid = $('dashboard-grid');
const widgetsJson = dashboardGrid.attr(':widgets-json');
const widgets = JSON.parse(widgetsJson);
console.log(widgets);
}
scrape();
Expected output: [{id: 1, name: "Volvo"}, {id: 2, name: "BMW"}]