从联合国UnData下载数据:Node.js获取1960-2015年多国人口数据
How to Download UNData Population Data with Node.js Using Your XML Query
Hey there, nice work getting that XML query from UNData's query builder—this is exactly what we need to interact with their SDMX API. Let's turn that into working code to fetch your 1960-2015 population data for target countries.
Prerequisites
- Node.js installed on your machine
- Your saved XML query file (let’s name it
undata-query.xmlfor this example)
Step 1: Set Up Your Project
First, create a new folder for your project and initialize npm:
mkdir undata-population-fetcher && cd undata-population-fetcher npm init -y
Install the dependencies we’ll need:
axiosfor making HTTP requestsfs-extrafor easy file reading/writing (we could use nativefs, but this simplifies things)
npm install axios fs-extra
Step 2: Write the Fetch Code
Create a file named fetch-data.js with the following code:
const axios = require('axios'); const fs = require('fs-extra'); // UNData SDMX API endpoint const UNDATA_API_URL = 'https://data.un.org/ws/rest/data'; async function fetchPopulationData() { try { // Read your saved XML query from file const xmlQuery = await fs.readFile('./undata-query.xml', 'utf8'); // Configure the API request const response = await axios.post(UNDATA_API_URL, xmlQuery, { headers: { 'Content-Type': 'application/xml', // Request JSON output for easier processing (change to 'application/xml' if you prefer XML) 'Accept': 'application/json' } }); // Save the full response data to a JSON file await fs.writeFile('./population-data.json', JSON.stringify(response.data, null, 2)); console.log('Success! Population data saved to population-data.json'); // Optional: Extract observations into a CSV for easier analysis const observations = response.data.dataSets[0].observations; const csvRows = ['Country,Year,Population']; // Map SDMX observation keys to meaningful values (adjust indices based on your query's dimensions) const dimensionMap = response.data.structure.dimensions.observation; Object.keys(observations).forEach(key => { const dimIndices = key.split(':'); const country = dimensionMap[0].values[dimIndices[0]].name; const year = dimensionMap[1].values[dimIndices[1]].name; const population = observations[key][0]; csvRows.push(`${country},${year},${population}`); }); await fs.writeFile('./population-data.csv', csvRows.join('\n')); console.log('CSV version saved to population-data.csv'); } catch (error) { console.error('Error fetching data:', error.message); if (error.response) { console.error('API Response Status:', error.response.status); console.error('API Response Details:', error.response.data); } } } // Run the function fetchPopulationData();
Step 3: Run the Code
Make sure your undata-query.xml file is in the same folder as fetch-data.js, then execute:
node fetch-data.js
Key Notes
- Adjust CSV Mapping: The code assumes your query’s observation dimensions are Country first, then Year. If your XML uses a different order, tweak the
dimensionMapindices by checking thestructuresection of the JSON response. - XML vs JSON Output: To get raw XML instead of JSON, change the
Acceptheader toapplication/xmland update the file-writing logic to save as an XML file. - Error Troubleshooting: If you run into issues, verify your XML query is valid by pasting it back into UNData’s query builder, and check for potential rate limits on the API.
提问来源:Stack Exchange,提问作者:user3808470




