本文主要是介绍获取最新中央气象台降水预报图片数据,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
获取最新中央气象台降水预报图片数据
JS版
import axios from "axios";
import * as cheerio from "cheerio";const BASE_URL = "http://www.nmc.cn";
const SOURCE_URL = BASE_URL + "/publish/precipitation/1-day.html";
const BASE_CLASS_NAME =".container .row .col-xs-10 .bgwhite .p-wrap .p-nav.nav2 ul li a";
const IMAGE_ID = "#imgpath";function getHour(url) {if (url != undefined) {let fileName = url.substring(url.lastIndexOf("_") + 1,url.lastIndexOf("."));let index = url.lastIndexOf(".")let hour = url.substring(index - 5, index) / 100;return hour;}return "";
}async function getBaseUrl() {const response = (await axios.get(SOURCE_URL)).data;const $ = cheerio.load(response);const featuredUrls = $(BASE_CLASS_NAME);let linkUrls = [];for (let i = 0; i < featuredUrls.length; i++) {let linkUrl = $(featuredUrls[i]).attr("href");let url = BASE_URL + linkUrl;linkUrls.push(url);}let imageUrlList = [];return axios.all(linkUrls.map((link) => axios.get(link))).then((resList) => {resList.forEach((res) => {const $ = cheerio.load(res.data);const imageDom = $(IMAGE_ID);let imageUrl = $(imageDom).attr("src");let hour = getHour(imageUrl);imageUrlList.push({hour,imageUrl,});});return imageUrlList;});
}let rainImageList = await getBaseUrl();
//console.log(rainImageList)
export default rainImageList;
JAVA版
这篇关于获取最新中央气象台降水预报图片数据的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!