js
// script.js// 地震数据接口地址
const earthquakeDataURL = 'https://www.chengdu.gov.cn/djj/jnds/syjb/sjcx/202301/t20230121_1614222.html';// 获取地震数据
async function getEarthquakes() {const response = await fetch(earthquakeDataURL);const data = await response.text();// 解析地震数据(例如,使用正则表达式或 HTML 解析器)// 以下是一个示例,使用正则表达式从数据中提取地震信息const earthquakePattern = /
/gs;const earthquakes = data.match(earthquakePattern);// 格式化地震信息,并添加到页面上earthquakes.forEach((earthquake) => {const earthquakeInfo = earthquake.match(/.?<\/td>/gs);// 从地震信息中提取相关字段(例如,日期、时间、震级、位置)const date = earthquakeInfo[0].slice(4, -5);const time = earthquakeInfo[1].slice(4, -5);const magnitude = earthquakeInfo[2].slice(4, -5);const location = earthquakeInfo[3].slice(4, -5);// 创建地震列表项const earthquakeItem = document.createElement('li');earthquakeItem.innerHTML = `
日期:${date}
时间:${time}
震级:${magnitude}
位置:${location}
`;// 将地震列表项添加到列表中document.getElementById('earthquake-list').appendChild(earthquakeItem);});
}// 定时自动更新数据
setInterval(getEarthquakes, 60000); // 每分钟更新一次// 初次加载页面时获取地震数据
getEarthquakes();