返回列表 上一主題 發帖

股票資料匯入,程式執行有問題

回復 5# caesar0125
我用IE8 抓取資料也有困難
改用      CreateObject("msxml2.xmlhttp") + CreateObject("htmlfile")
試看看
  1. Sub goodinfo_合併財報()
  2.     Dim xTable As Object, k As Integer, C As Integer, R As Integer, rc As Integer, sn As Integer
  3.     Dim url As String, i As Integer, E As Variant
  4.     Dim oXmlhttp As Object, oHtmldoc As Object, surl, op
  5.     'Sheets("總表").Select
  6.     rc = Sheets("總表").Cells(Rows.Count, 1).End(xlUp).Row
  7.     Sheets("營運績效").UsedRange.Clear
  8.     For i = 5 To rc
  9.         sn = Sheets("總表").Cells(i, 1)
  10.         url = "http://goodinfo.tw/StockInfo/StockBzPerformance.asp?STOCK_ID=" & sn & " &YEAR_PERIOD=10&RPT_CAT=M_YEAR"
  11.          Set oXmlhttp = CreateObject("msxml2.xmlhttp")
  12.          Set oHtmldoc = CreateObject("htmlfile")
  13.          With oXmlhttp
  14.             .Open "Get", url, False
  15.             .Send
  16.             oHtmldoc.write .responseText
  17.         End With
  18.         Set xTable = oHtmldoc.all.tags("TABLE")
  19.         With Sheets("營運績效")
  20.           '  .Cells.Clear
  21.             For Each E In Array(11, 13, 19) '11,13,19  "TABLE"
  22.                 k = k + 1
  23.                 For R = 0 To xTable(E).Rows.Length - 1
  24.                     For C = 0 To xTable(E).Rows(R).Cells.Length - 1
  25.                         .Cells(k, C + 1) = xTable(E).Rows(R).Cells(C).innertext
  26.                     Next
  27.                     k = k + 1
  28.                 Next
  29.             Next
  30.           End With
  31.     Next
  32. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 8# c_c_lai
可加上防錯
  1. Do
  2.             Set xTable = oHtmldoc.all.tags("TABLE")
  3.             Debug.Print xTable.Length, sn
  4.           ' If xTable.Length >= 19 Then Stop
  5.         Loop Until xTable.Length >= 19
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 10# c_c_lai
XP IE8 用 6#的程式碼,沒加上防錯的程式碼
沒有你所說的錯誤
  1. http://goodinfo.tw/StockInfo/StockBzPerformance.asp?STOCK_ID=1437 &YEAR_PERIOD=10&RPT_CAT=M_YEAR
  2. 24       1437
  3. http://goodinfo.tw/StockInfo/StockBzPerformance.asp?STOCK_ID=1438 &YEAR_PERIOD=10&RPT_CAT=M_YEAR
  4. 24       1438
  5. http://goodinfo.tw/StockInfo/StockBzPerformance.asp?STOCK_ID=1439 &YEAR_PERIOD=10&RPT_CAT=M_YEAR
  6. 24       1439
  7. http://goodinfo.tw/StockInfo/StockBzPerformance.asp?STOCK_ID=1440 &YEAR_PERIOD=10&RPT_CAT=M_YEAR
  8. 24       1440
  9. http://goodinfo.tw/StockInfo/StockBzPerformance.asp?STOCK_ID=1441 &YEAR_PERIOD=10&RPT_CAT=M_YEAR
  10. 24       1441
  11. http://goodinfo.tw/StockInfo/StockBzPerformance.asp?STOCK_ID=1442 &YEAR_PERIOD=10&RPT_CAT=M_YEAR
  12. 24       1442
  13. http://goodinfo.tw/StockInfo/StockBzPerformance.asp?STOCK_ID=1443 &YEAR_PERIOD=10&RPT_CAT=M_YEAR
  14. 24       1443
  15. http://goodinfo.tw/StockInfo/StockBzPerformance.asp?STOCK_ID=1444 &YEAR_PERIOD=10&RPT_CAT=M_YEAR
  16. 24       1444
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 13# c_c_lai
這網頁有流量管制.
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

本帖最後由 GBKEE 於 2016-6-12 15:02 編輯

回復 16# c_c_lai


   
url 裡為何我一直抓不到正確的 selectType 對應值?
  網頁的建置我沒入門,所以我也在摸索中


  .getElementsByName:      getElements 複數-> 物件的集合
  .getElementsBytagName:  getElements 複數-> 物件的集合
  .ALL.tags:                     tags 複數-> 物件的集合
  .getElementById :          getElement  單數 ->單一的物件
  1. Set xTable = oHtmldoc.ALL.tags("TABLE")
  2. Stop
  3. '看看區域變數視窗 xTable 的內容
  4.     Set xTable = oHtmldoc.ALL.tags("TABLE")(0)
  5. Stop
  6. '再次看看區域變數視窗 xTable 的內容
  7.     MsgBox xTable.INNERTEXT
複製代碼
附檔中有許多相同的程式碼.,,,,,,,,,,,
  1.      TVal = Array("MS", "", "0049", "0099P", "019919T", "0999", "0999P", "01", "02", "03", _
  2.                 "04", "05", "06", "07", "21", "22", "08", "09", "10", _
  3.                 "11", "12", "13", "24", "25", "26", "27", "28", "29", _
  4.                 "30", "31", "14", "15", "16", "17", "18", "23", "9299", "19", "20", "CB")
  5.     '**************************
  6.    If .ComboBox1.Value = "Select Name" Then
  7.             MsgBox ("您尚未選擇「產業類別」,請於" & vbCrLf & "確認後再次點選『開啟網頁』," & vbCrLf & "謝謝您!")
  8.             Exit Sub
  9.         End If        
  10.         For cts = 0 To UBound(lst)
  11.             If lst(cts) = .[D1] Then select2 = TVal(cts): Exit For
  12.         Next cts
複製代碼
可以整合一下
  1. Option Explicit
  2. Const MyUrl = "http://www.twse.com.tw/ch/trading/exchange/MI_MARGN/MI_MARGN.php"
  3. Private Sub Workbook_Open()
  4.     Dim oHtmldoc, op, cts
  5.     Set oHtmldoc = CreateObject("htmlfile")
  6.     With CreateObject("msxml2.xmlhttp")
  7.         .Open "Get", MyUrl, False
  8.         .Send
  9.         Do While .Status <> 200 Or .readyState <> 4: DoEvents: Loop
  10.         oHtmldoc.write .responseText
  11.     End With
  12.     Set op = oHtmldoc.all.tags("select")("selectType")
  13.     With Sheets("總表").ComboBox1
  14.         .Clear
  15.         For cts = 0 To op.Length - 1
  16.                 .AddItem                                             '清單方塊或下拉式清單方塊中加入一個項目
  17.                 .List(.ListCount - 1, 0) = op(cts).Text   ' 第1欄,最多9欄
  18.                 .List(.ListCount - 1, 1) = op(cts).Value ' 第2欄,最多9欄
  19.         Next
  20.         .Value = "Select Name"  '.List(0)  '0: 信用交易統計, 1:全部,2: 封閉式基金......
  21.     End With
  22. End Sub
複製代碼
  1. Private Function Select_Name() As Integer
  2.     With Sheets("總表").ComboBox1
  3.         If .ListIndex = -1 Then MsgBox ("您尚未選擇「產業類別」,請於" & vbCrLf & "確認後再次點選『開啟網頁』," & vbCrLf & "謝謝您!")
  4.         Select_Name = .ListIndex
  5.     End With
  6. End Function
複製代碼
  1. Sub 融資融券2()
  2.     Dim qdate As String, select2 As String
  3.     'Dim TVal(), cts As Integer
  4.     If Select_Name = -1 Then
  5.         Exit Sub
  6.      Else
  7.         With Sheets("總表")
  8.             qdate = Format(.[B1], "EE/MM/DD")
  9.             With .ComboBox1
  10.             select2 = .List(.ListIndex, 1)
  11.             End With
  12.         End With
  13.      End If   
  14.     With Sheets("融資_融券")
  15.         .Select
  16.         .Cells.Clear
  17.     End With
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

本帖最後由 GBKEE 於 2016-6-15 14:33 編輯

回復 15# c_c_lai
應運  網頁有流量管制.
1 檔案關閉
2重開檔案,   重啟adsl的連線,執行程式

另設一個要開開關關 goodinfo_合併財報檔案 的檔案
ThisWorkbook 模組的程式碼
  1. Option Explicit
  2. Private Sub Workbook_BeforeClose(Cancel As Boolean)
  3.     Application.Quit
  4. End Sub
  5. Private Sub Workbook_Open()
  6.     Dim App As New Application, xFile As String
  7.     xFile = "D:\goodinfo_合併財報.xls"  ' goodinfo_合併財報程式的檔案
  8.     Application.Visible = False
  9. Re_App:
  10.     With App
  11.         .Visible = True
  12.         .Workbooks.Open (xFile)
  13.     End With
  14.     If App.Visible = False Then
  15.     Set App = Nothing
  16.     GoTo Re_App   
  17.     End If
  18.     ThisWorkbook.Close
  19. End Sub
複製代碼
goodinfo_合併財報檔案 ThisWorkbook 模組的程式碼
  1. Option Explicit
  2. Private Sub Workbook_Open()
  3.     adsl連線   '參考   http://forum.twbts.com/thread-17716-1-1.html 的 adsl連線程式碼
  4.     goodinfo_合併財報
  5. End Sub
  6. Private Sub Workbook_BeforeClose(Cancel As Boolean)
  7.     Application.Quit
  8. End Sub
  9. Sub goodinfo_合併財報()   
  10.     Dim xTable As Object, k As Integer, C As Integer, sn As Double, r As Integer
  11.     Dim url As String, i As Double, E As Variant
  12.     Dim oXmlhttp As Object, oHtmldoc As Object, surl, op, tm
  13.    For Each E In ThisWorkbook.Names
  14.         If E.Name = "報表開始列" Then GoTo xStart
  15.    Next
  16.     ThisWorkbook.Names.Add "報表開始列", 1
  17.     ThisWorkbook.Names.Add "報表開時間", Time
  18.     ThisWorkbook.Activate
  19. xStart:
  20.     Sheets("營運績效").Activate
  21.         With Sheets("營運績效2")
  22.             .UsedRange.Clear
  23.         End With
  24.     For i = [報表開始列] To Sheets("總表").Cells(Rows.Count, 2).End(xlUp).Row '
  25. APP_EX:
  26.         sn = Sheets("總表").Cells(i, 2)
  27.         url = "http://goodinfo.tw/StockInfo/StockBzPerformance.asp?STOCK_ID=" & sn & "&YEAR_PERIOD=10&RPT_CAT=M_YEAR"
  28.         Set oXmlhttp = CreateObject("msxml2.xmlhttp")
  29.         Set oHtmldoc = CreateObject("htmlfile")
  30.         With oXmlhttp
  31.             .Open "Get", url, False
  32.             .Send
  33.             oHtmldoc.write .responseText
  34.         End With
  35.         Set xTable = oHtmldoc.all.tags("TABLE")
  36.            '*********可觀看 流量被管制 ************************************
  37.            Sheets("營運績效").Cells(i, 1).Resize(, 3) = Array(xTable.Length, sn, Time)
  38.            Sheets("營運績效").Cells(i, 1).Select
  39.            '************************************************************
  40.               If xTable.Length = 0 Then
  41.                  ThisWorkbook.Names.Add "報表開始列", i
  42.                  ThisWorkbook.Close True
  43.               End If
  44.             With Sheets("營運績效2")
  45.             For Each E In Array(11, 13, 19) '11,13,19  "TABLE"
  46.                 k = k + 1
  47.                 For r = 0 To xTable(E).Rows.Length - 1
  48.                 For C = 0 To xTable(E).Rows(r).Cells.Length - 1
  49.                     .Cells(k, C + 1) = xTable(E).Rows(r).Cells(C).innertext
  50.                 Next
  51.                 k = k + 1
  52.                 Next
  53.             Next
  54.         End With
  55.     Next
  56.     ThisWorkbook.Names("報表開始列").Delete
  57.     MsgBox "費時 " & Application.Text(Time - [報表開時間], "M分S秒") & " Finished Complete!"
  58. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 要批評別人時,先想想自己是否完美無缺。
返回列表 上一主題