Public
Edited
Jan 12, 2024
1 fork
13 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
params = {
let params = new URLSearchParams();
params.append('low_year', '2010');
params.append('high_year', '2019');
params.append('import_or_export', 'import');
params.append('country_code', 'MEX');
params.append('summarize', 'country');
params.append('filetype', 'html');
return params;
}
Insert cell
result = axios({
method: "post",
url:
"https://corsproxy.io/?" +
encodeURIComponent(
"https://armstrade.sipri.org/armstrade/html/export_values.php"
),
data: params
}).then((result) => result.data)
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
Insert cell
Insert cell
$ = cheerio.load(result)
Insert cell
options = ({
class: ".csvtable",
start: 11, // number of rows to be omitted at the beginning of the table
end: 2, // number of rows to be omitted at the end of the table
fields: 11 // Position of the field names in the html table
})
Insert cell
scrapedData = {
const scrapedData = [];

// colnames
const cols = $(options.class + " tr:nth-child(" + options.fields + ") td");
let colnames = [];
cols.each((index, element) => {
colnames.push($(element).text());
});
colnames[0] = "country"; // To avoid  

$(options.class + " tr").each((index, element) => {
// don't take first lines
if (index <= options.start - 1) return true;
// don't take last lines
if (index >= $(options.class + " tr").length - options.end) return true;
// create lines
const tds = $(element).find("td");
let row = {};
// interate
for (let i = 0; i < colnames.length; i++) {
row = Object.assign(row, { [colnames[i]]: $(tds[i]).text() });
}
// push
scrapedData.push(row);
});
return scrapedData;
}
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more