我正在尝试从美国劳工统计局的CPI数据中获取数据,可以从这里访问我的谷歌表格。
我使用 =IMPORTDATA 和其他(IMPORTXML 等)来获取此 CPI 列表,但它不起作用。我收到以下错误消息:“无法获取网址:https://download.bls.gov/pub/time.series/cu/cu.data.0.Current”
请帮忙!
运行脚本会产生:
劳工统计局
访问被拒绝
劳工统计局致力于根据既定时间表及时提供数据。自动检索程序(通常称为“机器人”或“机器人”)可能会导致延迟并干扰其他客户及时访问信息。因此,不符合 BLS 使用政策的机器人活动是被禁止的。
对于任何不便,我们深表歉意。如果您认为我们犯了错误,请联系我们。
代码可能是:
function importBLSData() {
const apiKey = 'YOUR_API_KEY'; // Replace with your actual API key
const url = `https://api.bls.gov/publicAPI/v2/timeseries/data/CUUR0000SA0?registrationkey=${apiKey}`;
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const options = {
method: 'get',
headers: {
'User-Agent': 'Mozilla/5.0'
}
};
const response = UrlFetchApp.fetch(url, options);
if (response.getResponseCode() !== 200) {
Logger.log('Error: ' + response.getContentText());
return;
}
const data = JSON.parse(response.getContentText());
if (data.status === "REQUEST_SUCCEEDED") {
const series = data.Results.series[0].data;
const rows = [["Year", "Period", "Value"]];
series.forEach(item => {
rows.push([item.year, item.period, item.value]);
});
sheet.clear();
sheet.getRange(1, 1, rows.length, rows[0].length).setValues(rows);
} else {
Logger.log('Error: ' + JSON.stringify(data));
}
}
A1中的公式:
=importBLSData()
但您需要API密钥:https://www.bls.gov/developers/home.htm