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

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more