返回列表 上一主題 發帖

[發問] 請問如何抓取氣象局opendata xml 問題

本帖最後由 GBKEE 於 2016-1-9 15:36 編輯

回復 2# stillfish00
回復 3# joey0415
綜合2位,使用表單呈現,可選擇地區,資料格式,時間. 匯入所要的氣象資料.

新增 UserForm(表單) 中加入ComboBox1, ComboBox2, ComboBox3 ,CommandButton1
  1. Option Explicit
  2. Dim xObject()
  3. Private Sub UserForm_Initialize()
  4.     xObject = Array(ComboBox1, ComboBox2, ComboBox3)
  5.     觀測查詢_設定
  6.      Com
  7.      ComboBox1.Value = ComboBox1.List(0)
  8.      ComboBox2.Value = ComboBox2.List(0)
  9. End Sub
  10. Private Sub ComboBox1_Change() '測  站
  11.      Com
  12. End Sub
  13. Private Sub ComboBox2_Change() '資料格式
  14.     Dim i  As Double
  15.     With ComboBox3
  16.         .Clear
  17.         Select Case ComboBox2.ListIndex
  18.             Case 0 '
  19.                 For i = Date - 1 To DateAdd("Q", -1, Date) Step -1
  20.                     .AddItem
  21.                     .List(.ListCount - 1) = Format(i, "YYYY-MM-DD")
  22.                 Next
  23.             Case 1
  24.                 i = Date
  25.                 Do
  26.                     .AddItem
  27.                     .List(.ListCount - 1) = Format(i, "YYYY-MM")
  28.                     i = DateAdd("M", -1, i)
  29.                 Loop Until Year(i) < Year(Date) - 1
  30.             Case 2
  31.                 For i = Year(Date) To Year(Date) - 1 Step -1
  32.                     .AddItem
  33.                     .List(.ListCount - 1) = i
  34.                 Next
  35.         End Select
  36.         If ComboBox2.ListIndex > -1 Then .Value = .List(0)
  37.     End With
  38.     Com
  39. End Sub
  40. Private Sub ComboBox3_Change()  '時  間
  41.     Com
  42. End Sub
  43. Private Sub Com() '
  44.     Dim E As Variant
  45.     With CommandButton1
  46.         .Enabled = True
  47.         For Each E In xObject
  48.             If E.ListIndex = -1 Then .Enabled = False '按紐(觀測資料查詢):不可用
  49.         Next
  50.     End With
  51. End Sub
  52. Private Sub CommandButton1_Click() '按紐(觀測資料查詢)
  53.      Dim surl As String, QT As QueryTable
  54.     'http://e-service.cwb.gov.tw/HistoryDataQuery/YearDataController.do?command=viewMain&station=467410&datepicker=2016
  55.     'http://e-service.cwb.gov.tw/HistoryDataQuery/MonthDataController.do?"
  56.     'http://e-service.cwb.gov.tw/HistoryDataQuery/DayDataController.do?
  57.     surl = "URL; http://e-service.cwb.gov.tw/HistoryDataQuery/"
  58.     Select Case ComboBox2.ListIndex
  59.         Case 0
  60.             surl = surl & "DayDataController.do?"
  61.         Case 1
  62.             surl = surl & "MonthDataController.do?"
  63.         Case 2
  64.             surl = surl & "YearDataController.do?"
  65.     End Select
  66.     surl = surl & "command=viewMain&station=" & ComboBox1.List(ComboBox1.ListIndex, 1)
  67.     surl = surl & "&datepicker=" & ComboBox3
  68.     With ActiveSheet
  69.         .Cells.Delete
  70.         Set QT = .QueryTables.Add(Connection:=surl, Destination:=.Range("$A$1"))
  71.         With QT
  72.             .WebFormatting = xlWebFormattingNone
  73.             .Refresh BackgroundQuery:=False
  74.             .Delete
  75.         End With
  76.     End With
  77.     Set QT = Nothing
  78. End Sub
  79. Private Sub 觀測查詢_設定()
  80.     Dim i As Double, E As Object, op As Object
  81.     Dim oXmlhttp As Object, oHtmldoc As Object, Url As String
  82.     Set oXmlhttp = CreateObject("msxml2.xmlhttp")
  83.     Set oHtmldoc = CreateObject("htmlfile")
  84.     Url = "http://e-service.cwb.gov.tw/HistoryDataQuery/QueryDataController.do?command=viewMain"   '
  85.     With oXmlhttp
  86.         .Open "Get", Url, False
  87.         .Send
  88.         oHtmldoc.write .responseText
  89.     End With
  90.     With oHtmldoc
  91.         Set E = .all.tags("SELECT")(0)
  92.         Set op = .all.tags("option")
  93.     End With
  94.     For i = 0 To op.Length - 1
  95.         If i <= E.Length - 1 Then
  96.             With ComboBox1
  97.                 .AddItem
  98.                 .List(.ListCount - 1, 0) = op(i).innertext  '測站:名稱
  99.                 .List(.ListCount - 1, 1) = op(i).Value      '測站:數值
  100.             End With
  101.         Else
  102.             With ComboBox2
  103.                 .AddItem
  104.                 .List(.ListCount - 1, 0) = op(i).innertext  '資料格式:名稱
  105.             End With
  106.         End If
  107.     Next
  108. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 5# c_c_lai

謝謝你的測試,2003可空一格。
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

本帖最後由 GBKEE 於 2016-3-4 14:19 編輯

回復 9# yehmengfeng
試試看
UserForm 模組的程式碼
UserForm 需制定控制項 CommandButton1,ComboBox1,ComboBox2
  1. Option Explicit
  2. Dim Sh(1 To 2) As Worksheet
  3. Private Sub UserForm_Initialize()
  4.     Set Sh(1) = Sheets.Add  '設定新增工作表
  5.     Sh(1).Visible = False   '隱藏工作表
  6.     觀測查詢_設定
  7. End Sub
  8. Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
  9.     Application.DisplayAlerts = False
  10.     Sh(1).Delete
  11.     Application.DisplayAlerts = True
  12. End Sub
  13. Private Sub 觀測查詢_設定()
  14.     Dim Url As String, i As Double, op As Object, xDate As Date
  15.     Dim oHtmldoc As Object
  16.     Set oHtmldoc = CreateObject("htmlfile")
  17.     Url = "http://e-service.cwb.gov.tw/HistoryDataQuery/QueryDataController.do?command=viewMain"   '
  18.     With CreateObject("msxml2.xmlhttp")
  19.         .Open "Get", Url, False
  20.         .Send
  21.         oHtmldoc.write .responseText
  22.     End With
  23.     With ComboBox1
  24.         For Each op In oHtmldoc.all.tags("SELECT")(0)
  25.             .AddItem
  26.             .List(.ListCount - 1, 0) = op.innertext  '測站:名稱
  27.             .List(.ListCount - 1, 1) = op.Value      '測站:數值
  28.         Next
  29.         .ListIndex = 0
  30.     End With
  31.     With ComboBox2
  32.         For i = 0 To -59 Step -1  '60個月份
  33.             xDate = DateAdd("M", i, Date)
  34.             .AddItem
  35.             .List(.ListCount - 1, 0) = Format(xDate, "EE年MM月")
  36.             .List(.ListCount - 1, 1) = DateSerial(Year(xDate), Month(xDate), 1)
  37.         Next
  38.         .ListIndex = 0
  39.     End With
  40. End Sub
  41. Private Sub CommandButton1_Click() '按紐(資料查詢)
  42.      Dim surl As String, QT As Range, Qdate As Date, Station As String, Msg As Boolean
  43.     'http://e-service.cwb.gov.tw/HistoryDataQuery/DayDataController.do?
  44.     surl = "URL;http://e-service.cwb.gov.tw/HistoryDataQuery/DayDataController.do?"
  45.     surl = surl & "command=viewMain&station=" & ComboBox1.List(ComboBox1.ListIndex, 1)
  46.     Station = ComboBox1.List(ComboBox1.ListIndex, 0) & "(" & ComboBox1.List(ComboBox1.ListIndex, 1) & ")" '工作表名稱
  47.     On Error GoTo Make_station:  '處裡錯誤: 測站不存在
  48.     Set Sh(2) = Sheets(Station)  '指定工作表(測站)
  49.     On Error GoTo 0              '有錯誤不處理了
  50.     Application.ScreenUpdating = False
  51.     For Qdate = ComboBox2.List(ComboBox2.ListIndex, 1) To DateAdd("m", 1, ComboBox2.List(ComboBox2.ListIndex, 1)) - 1
  52.         surl = "URL;http://e-service.cwb.gov.tw/HistoryDataQuery/DayDataController.do?command=viewMain&station=" & ComboBox1.List(ComboBox1.ListIndex, 1) & "&datepicker=" & Format(Qdate, "yyyy-m-dd")
  53.         If Qdate > Date Then Exit For    '日期大於當日
  54.         If Not Sh(2).Range("A:A").Find(Qdate, LookIn:=xlFormulas) Is Nothing Then GoTo Ne  '日期資料已存在,下一日期迴圈
  55.         Application.StatusBar = "匯入 " & Qdate & " 資料..."  '狀態列的文字
  56.         With Sh(1)
  57.             .UsedRange.Delete
  58.             With .QueryTables.Add(Connection:=surl, Destination:=.Range("$A$1"))
  59.                 .WebTables = "MyTable"
  60.                 .WebFormatting = xlWebFormattingNone
  61.                 .Refresh BackgroundQuery:=False
  62.                 If .ResultRange.Rows.Count > 5 Then  '有資料
  63.                     Set QT = .ResultRange  '含表頭的資料範圍
  64.                     With Sh(2)
  65.                         If .UsedRange.Count = 1 Then                      '工作表(測站)為空白
  66.                             .Range("A1") = "觀測時間"
  67.                             .Range("A1").Resize(5).Merge
  68.                         Else
  69.                             Set QT = QT.Rows("6:" & QT.Rows.Count)         '不含表頭的資料
  70.                         End If
  71.                         With .Cells(Rows.Count, "b").End(xlUp)
  72.                             If .Row = 1 Then                '工作表(測站)為空白
  73.                                 QT.Copy .Cells
  74.                             Else                            '.Row = 4 ->沒有資料但有表頭
  75.                                 QT.Copy .Cells(IIf(.Row = 4, 3, 2))
  76.                             End If
  77.                         End With
  78.                         With .Range(.Cells(Rows.Count, "A").End(xlUp).Offset(1).Address & ":A" & Sh(2).Cells(Rows.Count, "B").End(xlUp).Row)
  79.                             .Cells = Qdate                      'A欄寫上日期
  80.                             .NumberFormatLocal = "yyyy-mm-dd"   '給予格式
  81.                         End With
  82.                     End With
  83.                 End If
  84.                 .Delete
  85.             End With
  86.         End With
  87. Ne:
  88.     Next
  89.     Application.StatusBar = False
  90.     Application.ScreenUpdating = True
  91.      '排序
  92.     With Sh(2).UsedRange.Offset(5)
  93.         .Sort Key1:=.Range("A1"), Order1:=xlAscending, Key2:=.Range("B1"), Order2:=xlAscending, Header:=xlNo
  94.     End With
  95.     Exit Sub
  96. Make_station:
  97.     Sheets.Add(Sheet1).Name = Station  '新增工作表(測站)
  98.     Resume   '回到錯誤的程式碼繼續程式
  99. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 13# yehmengfeng
我使用2003 正常
你試試修正看看
  1. Private Sub CommandButton1_Click() '按紐(資料查詢)
  2.      Dim surl As String, QT As Range, Qdate As Date, Station As String, Msg As Boolean
  3.     surl = "URL;http://e-service.cwb.gov.tw/HistoryDataQuery/DayDataController.do?"
  4.     Stop
  5.     MsgBox ComboBox1
  6.     MsgBox ComboBox1.List(Me.ComboBox1.ListIndex, 1)  '加上ME是否可修正錯誤
  7.     MsgBox ComboBox1.List(ComboBox1.ListIndex, 1)
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 話多不如話少,話少不如話好。
返回列表 上一主題