返回列表 上一主題 發帖

[發問] 如何用vba下載證交所網頁(日收盤價及月平均收盤價)資料

[發問] 如何用vba下載證交所網頁(日收盤價及月平均收盤價)資料


原先可以直接用網址連結方式下載
http://www.twse.com.tw/ch/trading/exchange/STOCK_DAY_AVG/genpage/Report201602/201602_F3_1_8_2303.php?STK_NO=2303&myear=2016&mmon=01
自2月起就無法連結
各位先進請問要如何編寫vba去下載圖片中紅色框框的資料
(證交所網址:http://www.twse.com.tw/ch/trading/exchange/STOCK_DAY_AVG/STOCK_DAY_AVGMAIN.php)

回復 1# clianghot546
試試看
  1. Sub 個股日收盤價及月平均價CSV()

  2.     Dim xml As Object
  3.     Dim stream
  4.     Dim URL As String
  5.     年 = 2016
  6.     月 = 2
  7.     股票代碼 = 1101
  8.     Set xml = CreateObject("Microsoft.XMLHTTP") '用來取得網頁資料
  9.     Set stream = CreateObject("ADODB.stream")   'ADODB.stream   '用來儲存二進位檔案
  10.     URL = "http://www.twse.com.tw/ch/trading/exchange/STOCK_DAY_AVG/STOCK_DAY_AVGMAIN.php"
  11.     xml.Open "POST", URL, 0
  12.     xml.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  13.     xml.send "download=csv&query_year=" & 年 & "&query_month=" & 月 & "&CO_ID=" & 股票代碼
  14.     With stream
  15.         .Open
  16.         .Type = 1
  17.         .write xml.ResponseBody
  18.         'SaveToFile:檔案名稱已存在時會有錯誤,須先刪除已存在的檔案名稱
  19.         If Dir("D:\" & 股票代碼 & ".CSV") <> "" Then Kill "D:\" & 股票代碼 & ".CSV"
  20.         .SaveToFile ("D:\" & 股票代碼 & ".CSV")
  21.         .Close
  22.     End With
  23. End Sub
複製代碼

TOP

回復 2# joey0415
謝謝joey0415的回覆
已試過可行
但是能否有直接類似"從web"下載後直接匯入excel儲存格的語法

TOP

回復 3# clianghot546

下載csv ,匯入再刪除即可
  1. Sub 個股日收盤價及月平均價CSV()

  2.     Dim xml As Object
  3.     Dim stream
  4.     Dim URL As String
  5.     年 = 2016
  6.     月 = 2
  7.     股票代碼 = 2498
  8.     Set xml = CreateObject("Microsoft.XMLHTTP") '用來取得網頁資料
  9.     Set stream = CreateObject("ADODB.stream")   'ADODB.stream   '用來儲存二進位檔案
  10.     URL = "http://www.twse.com.tw/ch/trading/exchange/STOCK_DAY_AVG/STOCK_DAY_AVGMAIN.php"
  11.     xml.Open "POST", URL, 0
  12.     xml.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  13.     xml.send "download=csv&query_year=" & 年 & "&query_month=" & 月 & "&CO_ID=" & 股票代碼
  14.     With stream
  15.         .Open
  16.         .Type = 1
  17.         .write xml.ResponseBody
  18.         'SaveToFile:檔案名稱已存在時會有錯誤,須先刪除已存在的檔案名稱
  19.         If Dir(ThisWorkbook.Path & "\" & 股票代碼 & ".CSV") <> "" Then Kill ThisWorkbook.Path & "\" & 股票代碼 & ".CSV"
  20.         .SaveToFile (ThisWorkbook.Path & "\" & 股票代碼 & ".CSV")
  21.         .Close
  22.     End With
  23.    
  24.     Cells.Clear

  25.     With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & ThisWorkbook.Path & "\" & 股票代碼 & ".CSV", Destination:=Range("$A$1"))
  26.         .TextFileCommaDelimiter = True
  27.         .Refresh BackgroundQuery:=False
  28.         .Delete
  29.     End With
  30.    
  31.     Kill ThisWorkbook.Path & "\" & 股票代碼 & ".CSV"
  32.    
  33. End Sub
複製代碼

TOP

回復 4# joey0415
謝謝joey0415的指教
可以正常運作了

TOP

回復  clianghot546

下載csv ,匯入再刪除即可
joey0415 發表於 2016-2-4 15:12


再請教
原先的vba碼在WIN7+OFFICE2010可運行
在WIN10+OFFICE2016卻會卡在       .SaveToFile ("C:\Users1\CYUser\Downloads" & "\" & 股票代碼 & ".CSV")
如下圖

TOP

回復 6# clianghot546
  1. C:\Users1\CYUser\Downloads" & "\" & 股票代碼 & ".CSV")
複製代碼
我的程式碼沒有這個哦!

C:\Users1\CYUser\Downloads

路徑不對哦

TOP

回復 6# clianghot546

    WIN10+OFFICE2016卻會卡在       .SaveToFile ("C:\Users1\CYUser\Downloads" & "\" & 股票代碼 & ".CSV")

WIN10+OFFICE2016 的pc裡有這資料夾嗎?

試試看 建立資料
  1. Option Explicit
  2. Sub Ex()
  3.     Dim xPath As String, i As Integer, S As String
  4.     xPath = "C:\Users1\CYUser\Downloads"
  5.     For i = 2 To UBound(Split(xPath, "\"))
  6.         S = Mid(xPath, 1, InStr(xPath, Split(xPath, "\")(i)) - 1)
  7.         If Dir(S, vbDirectory) = "" Then MkDir S
  8.    Next
  9.    If Dir(xPath, vbDirectory) = "" Then MkDir xPath
  10.    MsgBox Dir(xPath, vbDirectory)
  11. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 7# joey0415
joey0415
根據您的回覆我有稍作修改以套用在我的電腦
若有冒犯請見諒

TOP

回復 8# GBKEE
謝謝GBKEE回覆
今日花時間從頭看一遍後發現有一處路徑未修改完全
現在可正常運作了

TOP

        靜思自在 : 我們要做好社會的環保,也要做好內心的環保。
返回列表 上一主題