返回列表 上一主題 發帖

新版股市公開資訊觀測站的資料抓到EXECL?

回復 2# xisun2002
程式區 找找有'http://mops.twse.com.tw/mops/web的資訊
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 5# pupai
每一網頁的建置不同,要看網頁內容修改程序的.
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 7# pupai


   
按下 圖片的網址 http://mops.twse.com.tw/server-j ... N=3&REPORT_ID=C
  1. Option Explicit
  2. Sub Ex()
  3. Dim URL As String, xCo_Id As String, xSyear As String, xSseason As String
  4. xCo_Id = "[" & """股票代號""" & "," & """2485""" & "]"                     '要求輸入網頁的參數:股票代號
  5. xSyear = "[" & """年度""" & "," & """" & Format(Date, "e") & """" & "]"    'Format(Date, "e")->中華民國的年度
  6. xSseason = "[" & """季別""" & "," & """" & Format(Date, "q") & """" & "]"       'Format(Date, "q")->當年度的季別
  7. URL = "URL;http://mops.twse.com.tw/server-java/t164sb01?step=1&CO_ID=" & xCo_Id & "&SYEAR=" & xSyear & "&SSEASON=" & xSseason & "&REPORT_ID=C"
  8.     With ActiveSheet.QueryTables.Add(Connection:=URL, Destination:=Range("A1"))
  9.         .AdjustColumnWidth = False                   '自動調整欄寬
  10.         .WebSelectionType = xlSpecifiedTables
  11.         .WebFormatting = xlWebFormattingNone
  12.         .WebTables = "2,3,4"                 '資產負債表,綜合損益表,現金流量表
  13.         .WebPreFormattedTextToColumns = True
  14.         .WebConsecutiveDelimitersAsOne = True
  15.         .WebSingleBlockTextImport = False
  16.         .WebDisableDateRecognition = False
  17.         .WebDisableRedirections = False
  18.         .Refresh BackgroundQuery:=False
  19.     End With
  20. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 10# BigDog
網址:公開資訊觀測站-->營運概況-->財務比率分析->採IFRSs後->財務分析資料
   
  1. Option Explicit
  2. Sub 公開資訊網頁()
  3.     Dim A As Object, E As Object
  4.     With CreateObject("InternetExplorer.Application")
  5.         .Visible = True
  6.         ' .Navigate "http://mops.twse.com.tw/mops/web/t164sb04"     '網址:綜合損益表
  7.         '.Navigate "http://mops.twse.com.tw/mops/web/stapap1"       '網址:董監事持股餘額明細資
  8.          .Navigate "http://mops.twse.com.tw/mops/web/t05st22"
  9.          '網址:公開資訊觀測站-->營運概況-->財務比率分析->採IFRSs後->財務分析資料
  10.         Do While .ReadyState <> 4 Or .Busy: DoEvents: Loop
  11.          '   .document.getElementById("isnew").Value = "false"       '選擇: 歷史資料
  12.             '註解上一行程式碼為 -> 選擇: 最新資料,不會執行 If 內程式碼
  13.             If .document.getElementById("isnew").Value = "false" Then
  14.              .document.getElementById("isnew").FireEvent ("onchange")
  15.                 .document.getElementById("year").Value = "102"       '年度
  16.                 .document.getElementById("season").Value = "01"    '綜合損益表:季別
  17.                 .document.getElementById("month").Value = "08"      '董監事持股餘額明細資料:月份
  18.             End If
  19.             '********************************************************************
  20.            For Each A In .document.getelementSbyTAGNAME("INPUT")
  21.                 'If A.Name = "co_id" Then A.Value = "2317"
  22.                  If A.Name = "co_id" Then A.Value = ActiveSheet.Range("A1") '儲存格:指定代號
  23.                  If A.Value = " 搜尋 " Then A.Click                     '按下 搜尋
  24.             Next
  25.             Do While .ReadyState <> 4 Or .Busy: DoEvents: Loop
  26.             For Each E In .document.ALL.TAGS("div")
  27.                 If E.ID = "table01" Then
  28.                     .document.body.innerHTML = E.outerHTML
  29.                     Do While .ReadyState <> 4 Or .Busy: DoEvents: Loop
  30.                     .ExecWB 17, 2       '  Select All
  31.                     .ExecWB 12, 2       '  Copy selection
  32.                     With ActiveSheet
  33.                         .UsedRange.Offset(1).Clear
  34.                         .Cells(2, 1).Select
  35.                         .PasteSpecial Format:="HTML"
  36.                     End With
  37.                 End If
  38.             Next
  39.        .Quit                          '關閉 IE
  40.     End With
  41. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 14# PKKO
慢慢的體會
  1. Option Explicit
  2. Sub Ex_網頁元素()
  3.     Dim i As Integer, E As Object
  4.     With CreateObject("InternetExplorer.Application")
  5.         .Visible = True
  6.         .Navigate "http://mops.twse.com.tw/mops/web/t164sb04"
  7.         
  8.         Do While .Busy Or .readyState <> 4: DoEvents: Loop
  9.         Stop
  10.         '在這網址輸入相關資料,再繼續執行下面的程式
  11.          
  12.         With .Document  '網頁的文件
  13.           '*** 這段程式碼可查看這網頁的元素內容
  14.             On Error Resume Next
  15.             For Each E In .all
  16.                 i = i + 1
  17.                 Cells(i, "a") = E.tagname   '控制項標記的名稱
  18.                 Cells(i, "b") = E.ID        '控制項標記的ID (惟一的)
  19.                 Cells(i, "c") = E.Name      '控制項標記的命名
  20.                 Cells(i, "d") = E.Value     '控制項標記的值
  21.                 Cells(i, "e") = E.innertext '控制項標記的文字
  22.                 Cells(i, "f") = E.Type      '控制項標記的類型
  23.                 Cells(i, "g") = E.href      '控制項標記的網址
  24.             Next
  25.           '****************************************
  26.         End With
  27.     '    .Quit        '關閉網頁
  28.     End With
  29.     '執行後到工作表上,查看在這網址輸入相關資料的控制項的屬性
  30. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

"xCo_Id = "K"                   '要求輸入網頁的參數:股票代號"……這一行該如何寫,或者有其它更好的方式
回復 22# chang0833

參考 這裡  改一下
  1. X = Application.InputBox("請輸入篩選關鍵字")
  2. If X = "" Or X = "False" Then Exit Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 25# chang0833
已修改 試試看
  1. Option Explicit
  2. Sub Ex()
  3.     Dim URL As String, xCo_Id As String, xSyear As String, X As Integer, Rng As Range
  4.     Dim xSseason As Variant
  5.     xCo_Id = Application.InputBox("請輸入股票代號", , 2303)         '預設為 2303
  6.     X = Application.InputBox("請輸入最新年度", , Year(Date) - 1911) '中華民國的年度
  7.    
  8.     With ActiveSheet
  9.         For Each xSseason In .QueryTables 'WEB查詢物件集合
  10.             xSseason.Delete
  11.         Next
  12.         For Each xSseason In .Names       'Name 物件的集合
  13.             .Names(xSseason.Name).Delete
  14.         Next
  15.         .UsedRange.Clear
  16.         Set Rng = .Range("a1") '指定工作表上 WEB查詢的位置
  17.     End With
  18.     '''''''''''''''''''
  19.     For Each xSseason In Array(2, 3, 4, 1) '迴圈xSseason => 2, 3, 4, 1
  20.         xSyear = "" & IIf(xSseason > 1, X - 2, X - 1)
  21.         URL = "URL;http://mops.twse.com.tw/server-java/t164sb01?step=1&CO_ID=" & xCo_Id & "&SYEAR=" & xSyear & "&SSEASON=" & xSseason & "&REPORT_ID=C"
  22.         With ActiveSheet.QueryTables.Add(Connection:=URL, Destination:=Rng)
  23.             .Name = xCo_Id & "-" & xSyear & "- 第 " & xSseason & " 季" 'WEB查詢的名稱
  24.             .AdjustColumnWidth = True                  '自動調整欄寬
  25.             .WebSelectionType = xlSpecifiedTables
  26.             .WebFormatting = xlWebFormattingNone
  27.             .WebTables = "2,3,4"                  '資產負債表,綜合損益表,現金流量表
  28.             .WebPreFormattedTextToColumns = True
  29.             .WebConsecutiveDelimitersAsOne = True
  30.             .WebSingleBlockTextImport = False
  31.             .WebDisableDateRecognition = False
  32.             .WebDisableRedirections = False
  33.             .Refresh BackgroundQuery:=False
  34.             With .ResultRange      'WEB查詢資料的範圍
  35.                 Set Rng = .Cells(.Rows.Count + 2, 1) '下一WEB查詢的位置
  36.             End With
  37.         End With
  38.     Next
  39. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 27# chang0833
試試看(全部)
  1. Option Explicit
  2. Sub Ex()
  3.     Dim URL As String, xCo_Id As String, X As Integer, Rng As Range
  4.     Dim E As Variant, xSyear As Integer, xSseason As Integer, D_Name As String
  5.    
  6.     With ActiveSheet
  7.         For Each E In .QueryTables 'WEB查詢物件集合
  8.             E.Delete
  9.         Next
  10.         For Each E In .Names       'Name 物件的集合
  11.             .Names(E.Name).Delete
  12.         Next
  13.         .UsedRange.Clear
  14.         Set Rng = .Range("a1") '指定工作表上 WEB查詢的位置
  15.     End With
  16.     xCo_Id = Application.InputBox("請輸入股票代號", , 2303)         '預設為 2303
  17.     X = Year(Date) - 1910                  '中華民國的年度
  18.     For xSyear = X To X - 3 Step -1        '迴圈:年度    '105->102
  19.     'For xSyear = X - 3 To X               '迴圈:年度    '102->105
  20.         For xSseason = 1 To 4 '             '迴圈:季別    '1,2,3,4
  21.             URL = "URL;http://mops.twse.com.tw/server-java/t164sb01?step=1&CO_ID=" & xCo_Id & "&SYEAR=" & xSyear & "&SSEASON=" & xSseason & "&REPORT_ID=C"
  22.             With ActiveSheet.QueryTables.Add(Connection:=URL, Destination:=Rng)
  23.                 .Name = xCo_Id & "_" & xSyear & "_第" & xSseason & "季" 'WEB查詢的名稱
  24.                 .AdjustColumnWidth = True                  '自動調整欄寬
  25.                 .WebSelectionType = xlSpecifiedTables
  26.                 .WebFormatting = xlWebFormattingNone
  27.                 .WebTables = "2,3,4"                  '資產負債表,綜合損益表,現金流量表
  28.                 .WebPreFormattedTextToColumns = True
  29.                 .WebConsecutiveDelimitersAsOne = True
  30.                 .WebSingleBlockTextImport = False
  31.                 .WebDisableDateRecognition = False
  32.                 .WebDisableRedirections = False
  33.                 .Refresh BackgroundQuery:=False
  34.                 If .ResultRange.Rows.Count = 2 Then '無資料
  35.                     D_Name = .Name                  'WEB查詢的名稱
  36.                     .Delete                         '刪除:WEB查詢
  37.                     With Rng.Parent
  38.                         For Each E In .Names
  39.                             If InStr(E.Name, D_Name) Then E.Delete '刪除:工作表上的名稱->WEB查詢的名稱
  40.                         Next
  41.                     End With
  42.                 Else
  43.                     With .ResultRange      'WEB查詢資料的範圍
  44.                         Set Rng = .Cells(.Rows.Count + 2, 1) '下一WEB查詢的位置
  45.                     End With
  46.                 End If
  47.             End With
  48.         Next
  49.     Next
  50.     MsgBox "Ok"
  51. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 31# c_c_lai
這網頁有點奇怪,年度的選項為何有下一年度(未來的年度).
有人可分享嗎?
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

本帖最後由 GBKEE 於 2015-11-16 05:28 編輯

回復 34# chang0833
或是附檔 需要哪些資料
  1. Next
  2.     '''加上這段試試看
  3.     With Rng.Parent
  4.          .Range("A:A").SpecialCells(xlCellTypeBlanks).Delete xlShiftUp
  5.          '刪除:工作表上的空白列
  6.     End With
  7.     ''''''''''''''''
  8.     MsgBox "Ok"
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 【為善競爭】人生要為善競爭,分秒必爭。
返回列表 上一主題