返回列表 上一主題 發帖

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

請問我是想將證交所的每日收盤行情(不含權證牛熊證),貼上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

OK 了.... 謝謝指導

TOP

回復 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

        靜思自在 : 知識要用心體會,才能變成自己的智慧。
返回列表 上一主題