返回列表 上一主題 發帖

[發問] VBA如何處理網頁中的選擇項使網頁更新

回復 1# Scott090

http://e-service.cwb.gov.tw/HistoryDataQuery/QueryDataController.do?command=doQueryStation&station_no=467610


    例如上面就是「成功」站的資料,結果如下
成功 (467610) |121°21' 55〞E|23°05' 57〞N|33.5M|臺東縣成功鎮公民路84號|中央氣象局

你只要把你想要的資料代碼改變就會有不同的地區資料

例如臺中 (467490)
http://e-service.cwb.gov.tw/HistoryDataQuery/QueryDataController.do?command=doQueryStation&station_no=467490
臺中 (467490) |120°40' 33〞E|24°08' 51〞N|84.04M|臺中市北區精武路295號|中央氣象局

TOP

回復 3# Scott090

都找到代碼就不要花心思了!只要改代碼就會知道溫度,把溫度抓下來即可

TOP



就把區域代碼改一下放在上就知道案了

完全不用等
  1. Sub EX()
  2. finalrow=Cells(Rows.Count, 1).End(xlUp).Row
  3.     For i = 1 To finalrow
  4.         s = Cells(i, 1)
  5.         URL = "http://e-service.cwb.gov.tw/HistoryDataQuery/QueryDataController.do?command=doQueryStation&station_no=" & s
  6.         With CreateObject("InternetExplorer.Application")
  7.             .Visible = False     '  是否顯示 IE
  8.             .Navigate URL
  9.                 Do While .ReadyState <> 4
  10.                     DoEvents
  11.                 Loop
  12.             Set ieDoc = .Document
  13.             Cells(i, 2) = ieDoc.body.innerText
  14.             .Quit
  15.         End With
  16.     Next
  17.     Application.StatusBar = False
  18. End Sub
複製代碼

TOP

本帖最後由 joey0415 於 2015-1-14 10:14 編輯

回復 6# Scott090

改好了!為了這個又學到東西

不過要花了好多時間
  1. Sub 觀測資料查詢系統()

  2.        ActiveSheet.Cells.Clear
  3.        URLb = "http://e-service.cwb.gov.tw/HistoryDataQuery/index.jsp"
  4.        Set ie = CreateObject("internetexplorer.application")
  5.        ie.Visible = True
  6.        ie.Navigate URLb
  7.        Do While ie.readyState <> 4 Or ie.Busy: DoEvents: Loop
  8.         Application.Wait Now + TimeValue("00:00:01") '等待頁面,過快會找不到資料
  9.         x = ie.document.all.tags("option").Length '查詢select共有幾項
  10.       Stop
  11.            For i = 0 To x '該網頁的option選項從0-35

  12.         '---------,內建的fireevent 的onchange失效,改用調用js的方法--------
  13.         Set evt = ie.document.createEvent("HTMLEvents")
  14.         evt.initEvent "change", True, False
  15.         Set lst = ie.document.getElementById("station") 'option的id是station
  16.         lst.selectedIndex = i '自動循環option的值,找到index
  17.         lst.dispatchEvent evt

  18.          Do While ie.readyState <> 4 Or ie.Busy: DoEvents: Loop
  19.             Application.Wait Now + TimeValue("00:00:01")
  20. '            Cells(i + 1, 1) = Trim(ie.document.getelementsbytagname("table")(1).innertext)'若找talbe是第一個,但空白過多要處理
  21.             Cells(i + 1, 1) = Trim(ie.document.getelementsbytagname("td")(4).innertext) '若找td是第4個下面亦同
  22.             Cells(i + 1, 2) = Trim(ie.document.getelementsbytagname("td")(5).innertext)
  23.             Cells(i + 1, 3) = Trim(ie.document.getelementsbytagname("td")(6).innertext)
  24.             Cells(i + 1, 4) = Trim(ie.document.getelementsbytagname("td")(7).innertext)
  25.             Cells(i + 1, 5) = Trim(ie.document.getelementsbytagname("td")(8).innertext)
  26.             Cells(i + 1, 6) = Trim(ie.document.getelementsbytagname("td")(9).innertext)
  27.        Next
  28.                ie.Quit
  29.               ActiveSheet.Cells.EntireColumn.AutoFit     '  自動調整欄寬
  30.    End Sub
複製代碼

2015-01-14_101343.gif (158.69 KB)

2015-01-14_101343.gif

TOP

        靜思自在 : 原諒別人就是善待自己。
返回列表 上一主題