返回列表 上一主題 發帖

請問如何用 MSXML2.XMLHTTP 捉 臺灣證券交易所 個股月成交資訊

回復 1# wufonna
試試看
  1. Option Explicit
  2. Sub Ex下載CSV()
  3.     Dim xml As Object
  4.     Dim stream
  5.     Dim URL As String     '目的網址
  6.     Set xml = CreateObject("Microsoft.XMLHTTP") '用來取得網頁資料
  7.     Set stream = CreateObject("ADODB.stream")   'ADODB.stream   '用來儲存二進位檔案
  8.     URL = "http://www.twse.com.tw/ch/trading/exchange/STOCK_DAY/STOCK_DAY_print.php?genpage=genpage/Report201408/201408_F3_1_8_2485.php&type=csv"
  9.     xml.Open "get", URL, 0
  10.     xml.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  11.     xml.send
  12.     With stream
  13.         .Open
  14.         .Type = 1
  15.         .write xml.ResponseBody
  16.         'SaveToFile:檔案名稱已存在時會有錯誤,須先刪除已存在的檔案名稱
  17.         If Dir("D:\2485.CSV") <> "" Then Kill "D:\2485.CSV"
  18.         .SaveToFile ("D:\2485.CSV")
  19.         .Close
  20.     End With
  21. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

本帖最後由 GBKEE 於 2014-10-2 05:50 編輯

回復 3# wufonna
換一個方式
  1. Option Explicit
  2. Sub Ex_個股月成交資訊()
  3.     Dim i As Integer, S As Integer, k As Integer, A As Object, ii, j
  4.     With CreateObject("InternetExplorer.Application")
  5.        ' .Visible = True
  6.         .Navigate "http://www.twse.com.tw/ch/trading/exchange/STOCK_DAY/STOCK_DAY_print.php?genpage=genpage/Report201408/201408_F3_1_8_2485.php&type=list"
  7.         Do While .Busy Or .readyState <> 4: DoEvents: Loop
  8.         Ep .document.getElementsByTagName("table")(1).outerHTML
  9.         .Quit        '關閉網頁
  10.     End With
  11. End Sub
  12. Sub Ep(S As String)
  13.     Dim D As New DataObject
  14.     'DataObject 物件 在進行轉換動作時,做為格式化文字資料的暫存區域。其也可以暫存和儲存在 DataObject 的文字片段相關的格式。
  15.     '宣告 Dim D As New DataObject '須在工具-> 設定引用項目加入 新增引用 Microsoft Forms 2.0 Object Library ,或於專案 加入一表單即可
  16.     With D
  17.         .SetText S
  18.         .PutInClipboard
  19.         With ActiveSheet
  20.             .UsedRange.Clear
  21.             .Range("a1").Select
  22.             .PasteSpecial Format:="Unicode 文字"
  23.         End With
  24.     End With
  25. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 6# wufonna
Set  CreateObject("InternetExplorer.Application")=Nothing
不是變數是一個實際的物件.
Nothing 關鍵字是用來將一個物件變數從一個實際的物件�堣擢鰶}來。使用 Set 陳述式可指定 Nothing 給物件變數。

修改看看
  1. Option Explicit
  2. Dim IE As Object  '宣告為這模組的私用變數(這模組中的程序可呼叫的變數)
  3. Sub Index()
  4. Dim i As Integer, I2 As Integer
  5. Dim x, y, XX, YY, ZZ
  6. Dim k
  7. Set IE = CreateObject("InternetExplorer.Application")
  8. Const AA As Integer = 5
  9. 工作表3.Activate
  10. '工作表3.Select
  11. With 工作表1
  12. ''''''''未測試
  13.     For i = 2 To .Range("A" & .Rows.Count).End(xlUp).Row   
  14. Call Ex_1(Split(Date, "/")(0) & Format((Split(Date, "/")(1)), "00"), I2)
  15.     Next
  16. '''''''未測試
  17. End With
  18. End If
  19. IE.Quit  '在Sub Index() 程式結束前關閉網頁
  20. End Sub
  21. Sub Ex_1(SS As String, MM As Integer)
  22.     Dim i As Integer, S As Integer, k As Integer, A As Object, II, j
  23. '    工作表3.Select
  24.     With IE
  25.        ' .Visible = True
  26.        On Error Resume Next
  27.         .Navigate "http://www.twse.com.tw/ch/trading/exchange/STOCK_DAY/STOCK_DAY_print.php?genpage=genpage/Report" & SS & "/" & SS & "_F3_1_8_" & MM & ".php&type=list"
  28.         Do While .Busy Or .readyState <> 4: DoEvents: Loop
  29.         Ep .document.getElementsByTagName("table")(1).outerHTML
  30.        ' .Quit        '關閉網頁
  31.     End With
  32. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 9# wufonna


IE.Quit  '在Sub Index() 程式結束前關閉網頁
沒有IE.Quit ,關閉Excel應用程式後IE還是開啟的,因IE.Visible = False (物件沒有顯示.)
Set IE = Nothing,釋放了變數所結合的物件,Excel應用程式中,可減少佔用的的記憶體
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 【生命在呼吸間】佛陀說:「生命在呼吸間。」人無法管住自己的生命,更無法擋住死期,讓自己永住人間。既然生命去來這麼無常,我們更應該好好地愛惜它、利用它、充實它,讓這無常、寶貴的生命,散發它真善美的光輝,映照出生命真正的價值。
返回列表 上一主題