3、选择数据源:
如果网页是 HTML 表格,Power Query 会自动识别并显示可用的表格。你可以选择需要的数据部分。
4、加载数据到 Excel:
点击 `加载` 按钮,将数据加载到 Excel 工作表中。
5、自动更新数据:
数据加载后,你可以通过点击 `数据` `刷新所有` 来手动刷新数据。
如果你需要定期自动刷新,可以在 Power Query 编辑器中设置计划任务(例如每天或每周)。
2、使用 VBA 脚本自动更新网页数据
如果你熟悉 VBA(Visual Basic for Applications),你可以编写脚本来自动从网页抓取数据并更新 Excel。
示例代码:
vba
Sub UpdateWebData()
Dim http As Object
Dim html As Object
Dim webpage As String
Dim table As Object
Dim row As Object
Dim cell As Object
Dim i As Integer, j As Integer
' 解析 HTML 内容
Set html = CreateObject(htmlfile)
html.body.innerHTML = http.responseText
' 假设我们需要抓取表格数据
Set table = html.getElementById(table_id) ' 替换为实际的表格 ID
' 清空现有数据
Sheets(1).Cells.ClearContents
' 开始填充数据
i = 1
For Each row In table.Rows
j = 1
For Each cell In row.Cells
Sheets(1).Cells(i, j).Value = cell.innerText
j = j + 1
Next cell
i = i + 1
Next row