上海有色今日金价 (上海有色金属网今日行情)

上海休闲 08-04 阅读:2 评论:18
上海有色今日金价 (上海有色金属网今日行情)
时间 金价(元/克) 变动幅度(%)
javascript // script.js// 获取上海有色金属网今日金价数据 fetch('https://www.smcx.com/metals/history/au_today.html', {method: 'GET', }).then((response) => {return response.text();}).then((data) => {// 解析 HTML 数据const parser = new DOMParser();const doc = parser.parseFromString(data, 'text/html');// 从 HTML 中提取金价数据const table = doc.querySelector('table.today-price-table');const rows = table.querySelectorAll('tr');// 创建金价数据数组const goldPrices = [];for (const row of rows) {const cells = row.querySelectorAll('td');const time = cells[0].textContent;const price = cells[1].textContent;const change = cells[2].textContent;goldPrices.push({ time, price, change });}// 填充表格数据const tbody = document.querySelector('tbody');for (const goldPrice of goldPrices) {const newRow = document.createElement('tr');const newCell1 = document.createElement('td');const newCell2 = document.createElement('td');const newCell3 = document.createElement('td');newCell1.textContent = goldPrice.time;newCell2.textContent = goldPrice.price;newCell3.textContent = goldPrice.change;newRow.appendChild(newCell1);newRow.appendChild(newCell2);newRow.appendChild(newCell3);tbody.appendChild(newRow);}// 创建图表const chart = Highcharts.chart('chart', {title: {text: '上海有色今日金价'},xAxis: {categories: goldPrices.map((goldPrice) => goldPrice.time)},yAxis: {title: {text: '金价(元/克)'}},series: [{name: '金价',data: goldPrices.map((goldPrice) => parseFloat(goldPrice.price))}]});});
版权声明

本文仅代表作者观点,不代表上海桑拿立场。
本文系作者授权发表,未经许可,不得转载。