使用Node编程语言编写代码,可以获取并下载Bing每日图片。通过调用Bing的API接口,我们可以获取到每日更新的精美图片,并将其保存到本地。以下是使用Node编写的代码示例,供大家参考:
const axios = require('axios');
const fs = require('fs');
async function downloadBingDailyImage() {
try {
const response = await axios.get('https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1');
const imageData = response.data.images[0];
const imageUrl = `https://www.bing.com${imageData.url}`;
const imageTitle = imageData.title;
const imageResponse = await axios.get(imageUrl, { responseType: 'stream' });
const imageFilePath = `./bing-daily-image/${imageTitle}.jpg`;
const writeStream = fs.createWriteStream(imageFilePath);
imageResponse.data.pipe(writeStream);
return `成功下载Bing每日图片,保存路径为:${imageFilePath}`;
} catch (error) {
console.error('下载Bing每日图片失败:', error);
return '下载Bing每日图片失败';
}
}
downloadBingDailyImage();
暂无评论