返回列表 上一主題 發帖

[發問] 現在要如何到證交所下載外資成交資訊

[發問] 現在要如何到證交所下載外資成交資訊

之前是用下列的程式碼 到 http://www.twse.com.tw/ch/trading/fund/TWT38U/TWT38U.php 這邊去下載每日外資買賣超資訊。
原本都沒有問題,但一月份 證交所改網頁,原本可以看到一個下載路徑,但現在沒辦法看到,導致不知道該如何下載每日的 CSV 檔...

有沒有人知道該如何下載?

Dim st
Dim myURL As String
Dim oStream As Object           'ADODB.Stream
Dim WinHttpReq As Object            'XMLHTTP
Dim fileidx As String
Dim seldate As String

Worksheets("Config").Select
endline = Range("A65536").End(xlUp).Row


    seldate = Range("A" & i).Value    ''成交日期
    fileidx = seldate
   
    ''http://www.twse.com.tw/ch/trading/fund/TWT38U/TWT38U_print.php?edition=ch&filename=genpage/A20141202.dat&type=csv
   
    myURL = "http://www.twse.com.tw/ch/trading/fund/TWT38U/TWT38U_print.php?edition=ch&filename=genpage/A" & _
            fileidx & ".dat&type=csv"
         
    ''外資買賣資訊
    Set WinHttpReq = CreateObject("MSXML2.XMLHTTP")
    With WinHttpReq
        .Open "GET", myURL, False
        .Send
        myURL = .responseText
    End With
    Set oStream = CreateObject("ADODB.Stream")
    With oStream
        .Open
        .Type = 1
        .Write WinHttpReq.responseBody
        fileidx = Sheets("Config").Range("G2") & "\A" & fileidx & ".csv"          ''設定下載路徑及檔名
        On Error Resume Next
        Kill fileidx
        On Error GoTo 0
        .SaveToFile fileidx
        .Close
    End With
    Set WinHttpReq = Nothing
    Set oStream = Nothing

End Sub

回復 1# vanguarx
改用Post , 大概如下,輸入日期和儲存路徑自己改成你要的吧~
  1. Sub TestWeb()
  2.     Dim myURL As String
  3.     Dim oStream As Object           'ADODB.Stream
  4.     Dim WinHttpReq As Object            'XMLHTTP
  5.     Dim fileidx As String
  6.     Dim sPost As String
  7.     Dim dteQueryDate As Date
  8.    
  9.     dteQueryDate = #3/11/2015#
  10.    
  11.     myURL = "http://www.twse.com.tw/ch/trading/fund/TWT38U/TWT38U.php"
  12.     sPost = "download=csv&qdate=" & Format(dteQueryDate, "e/mm/dd") & "&sorting=by_issue"
  13.     sPost = Replace(sPost, "/", "%2F")  'or urlencode
  14.    
  15.     ''外資買賣資訊
  16.     Set WinHttpReq = CreateObject("MSXML2.XMLHTTP")
  17.     With WinHttpReq
  18.         .Open "POST", myURL, False
  19.         .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  20.         .setRequestHeader "Content-Length", Len(sPost)
  21.         .Send (sPost)
  22.     End With
  23.    
  24.     Set oStream = CreateObject("ADODB.Stream")
  25.     With oStream
  26.         .Open
  27.         .Type = 1
  28.         .Write WinHttpReq.responseBody
  29.         fileidx = ThisWorkbook.Path & "\" & Format(dteQueryDate, "yyyymmdd") & ".csv"          ''設定下載路徑及檔名
  30.         On Error Resume Next
  31.         Kill fileidx
  32.         On Error GoTo 0
  33.         .SaveToFile fileidx
  34.         .Close
  35.     End With
  36.    
  37.     Set WinHttpReq = Nothing
  38.     Set oStream = Nothing
  39. End Sub
複製代碼
表達不清、題意不明確、沒附檔案格式、沒有討論問題的態度~~~~~~以上愛莫能助。

TOP

OK 了.... 謝謝指導

TOP

請問我是想將證交所的每日收盤行情(不含權證牛熊證),貼上excel,現在用post法該怎麼寫的
原本get法是這樣寫的
Sub getSIIPrice()
    urlStr = "URL;http://www.twse.com.tw/ch/trading/exchange/MI_INDEX/genpage/Report" & Format(Sheets("UI").Range("A21").Value, "YYYYMM") & "/A112" & Format(Sheets("UI").Range("A21").Value, "YYYYMMDD") & "ALLBUT0999_1.php?select2=ALLBUT0999&chk_date=" & Year(Sheets("UI").Range("A21").Value) - 1911 & "/" & Format(Sheets("UI").Range("A21").Value, "MM/DD")
    Sheets("SIIPrice").Select
    Cells.Select
    Selection.Delete Shift:=xlUp
    With ActiveSheet.QueryTables.Add(Connection:= _
        urlStr _
        , Destination:=Range("A1"))
        .Name = "19"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlSpecifiedTables
        .WebFormatting = xlWebFormattingNone
        .WebTables = "10"
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
        .Delete
    End With
End Sub

後來我將網址改為http://www.twse.com.tw/ch/trading/exchange/MI_INDEX/MI_INDEX.php
ActiveSheet.QueryTables.Add增加一個PostText參數卻不行
請問該如何更改呢?

TOP

回復 4# adranimal
urlstr = "URL;http://www.twse.com.tw/ch/trading/exchange/MI_INDEX/MI_INDEX.php"
.WebTables = "2"
.PostText = "download=&qdate=104%2F03%2F12&selectType=ALLBUT0999"

改這三個地方
表達不清、題意不明確、沒附檔案格式、沒有討論問題的態度~~~~~~以上愛莫能助。

TOP

感謝S大回覆
目前改成這樣
Sub getSIIPrice()

    urlStr = "URL;http://www.twse.com.tw/ch/trading/exchange/MI_INDEX/MI_INDEX.php"
    Sheets("SIIPrice").Select
    Cells.Select
    Selection.Delete Shift:=xlUp
  With ActiveSheet.QueryTables.Add(Connection:= _
    urlStr _
     , Destination:=Range("A1"))
      .WebTables = "2"
      .PostText = "downlod=&qdate=104%2F03%2F12&selectType=ALLBUT0999"
      .Name = "19"
      .FieldNames = True
      .RowNumbers = False
      .FillAdjacentFormulas = False
      .PreserveFormatting = True
      .RefreshOnFileOpen = False
      .BackgroundQuery = True
      .RefreshStyle = xlInsertDeleteCells
      .SavePassword = False
      .SaveData = True
      .AdjustColumnWidth = True
      .RefreshPeriod = 0
      .WebSelectionType = xlSpecifiedTables
      .WebFormatting = xlWebFormattingNone
      .WebTables = "10"
      .WebPreFormattedTextToColumns = True
      .WebConsecutiveDelimitersAsOne = True
      .WebSingleBlockTextImport = False
      .WebDisableDateRecognition = False
      .WebDisableRedirections = False
      .Refresh BackgroundQuery:=False
      .Delete
   End With
End Sub

但是沒有回傳資料,不知問題出在哪?

TOP

回復 6# adranimal
仔細點啊,你.WebTables 屬性改了兩次
表達不清、題意不明確、沒附檔案格式、沒有討論問題的態度~~~~~~以上愛莫能助。

TOP

回復 7# stillfish00
感謝S大的指教
已經可以使用了
但是如果我想要讓抓檔的日期等於Sheets("UI").Range("A22").Value 應該怎麼編寫呢?

TOP

回復 8# adranimal
一二樓都有線索,自己做做看
表達不清、題意不明確、沒附檔案格式、沒有討論問題的態度~~~~~~以上愛莫能助。

TOP

謝謝stillfish00大大的程式碼
順利下載證交所的外資買賣超資訊
另外也自行改成下載投信買賣超資訊
都能正常使用非常感謝

但是想另外請問
以下兩個網頁是櫃買中心的網頁
1.三大法人買賣明細資訊 :
http://www.tpex.org.tw/web/stock/3insti/daily_trade/3itrade_hedge.php?l=zh-tw
2.上櫃股票每日收盤行情(不含定價)
我需要的是所有證券(不含權證.牛熊證)
http://www.tpex.org.tw/web/stock/aftertrading/otc_quotes_no1430/stk_wn1430.php?l=zh-tw

我自行修改了網址的部分
結果抓回來的csv檔
內容卻是網頁的原始碼
請問是不是下列這段需要做何調整

Set WinHttpReq = CreateObject("MSXML2.XMLHTTP")
    With WinHttpReq
        .Open "POST", myURL, False
        .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        .setRequestHeader "Content-Length", Len(sPost)
        .send (sPost)
    End With

或是該如何調整
使得能夠下載回csv檔
感謝

TOP

        靜思自在 : 稻穗結得越飽滿,越會往下垂,一個人越有成就,就要越有謙沖的胸襟。
返回列表 上一主題