返回列表 上一主題 發帖

[發問] 查找網站資料

回復 23# GBKEE

請問GBKEE大
執行時出現錯誤 , 該如何修正呢?

ScreenHunter_02 Aug. 13 22.57.jpg (13.16 KB)

錯誤訊息

ScreenHunter_02 Aug. 13 22.57.jpg

ScreenHunter_02 Aug. 13 22.56.jpg (26.32 KB)

ScreenHunter_02 Aug. 13 22.56.jpg

TOP

回復 23# GBKEE

讚!謝謝版主用這麼功整的語法,小弟學習啦!

TOP

回復 13# joey0415
給你參考
  1. Option Explicit
  2. Sub 股票日報表()                        '查詢單一股票
  3.     Dim 股票代號 As String, 頁數 As String, T As Date
  4.     Do While 股票代號 = ""
  5.         股票代號 = InputBox("股票代號", "輸入查詢之股票代號", "1101")
  6.         If 股票代號 = "" Then End
  7.     Loop
  8.     T = Time
  9.     頁數 = 報表頁數(股票代號)
  10.     If 頁數 <> "" Then
  11.         匯入日報表 股票代號, 頁數
  12.        MsgBox Format(Time - T, "完成 費時 HH:MM:SS")
  13.     Else
  14.         MsgBox "股票代號  " & 股票代號 & " 有誤 !!!"
  15.     End If
  16. End Sub
  17. Private Sub 匯入日報表(股票代號 As String, 頁數 As String)      '參數傳送來的 --股票代號 & 頁數
  18.     Dim Rng As Range
  19.     With ActiveSheet
  20.         If .QueryTables.Count = 0 Then
  21.             With .QueryTables.Add("URL;about:Tabs", .[A1])
  22.                 .Refresh BackgroundQuery:=False
  23.             End With
  24.         End If
  25.         With .QueryTables(1)
  26.             .Connection = "URl;http://bsr.twse.com.tw/bshtm/bsContent.aspx?StartNumber=" & 股票代號 & "&FocusIndex=All_" & 頁數
  27.             .WebFormatting = xlWebFormattingNone
  28.             .WebTables = "4,""table2"""
  29.             .Refresh False
  30.         End With
  31.         Set Rng = .UsedRange.Offset(6)
  32.          With Rng.Columns("A")
  33.             .Replace "序", "=Why", xlWhole
  34.             .SpecialCells(xlCellTypeFormulas, xlErrors).Value = ""
  35.             .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
  36.         End With
  37.         .Cells(1).Select
  38.     End With
  39. End Sub
  40. Private Function 報表頁數(股票代號 As String)   '參數傳送來的 --股票代號
  41.     With CreateObject("InternetExplorer.Application")
  42.             .Navigate "http://bsr.twse.com.tw/bshtm/bsMenu.aspx"
  43.             Do While .Busy Or .readyState <> 4
  44.                 DoEvents
  45.             Loop
  46.             .document.getElementsByName("txtTASKNO").Item(0).Value = 股票代號
  47.             .document.getElementsByName("btnOK")(0).Click
  48.             Do While .Busy Or .readyState <> 4
  49.                 DoEvents
  50.             Loop
  51.             報表頁數 = .document.getElementsByName("sp_ListCount")(0).innertext
  52.             .Quit
  53.     End With
  54. End Function
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

謝謝兩位的分享與教導~

TOP

回復 20# joey0415
謝謝超版解決這個問題!雖然我不是用這個方法抓的!不過還是要感謝

可否PO上來交流.
.all(5)的5我是一個一個試出來的
超版也是一個一個試出來的嗎?
還是有別的語法可以list出來呢?

用IE語法讀取網頁資料,我也是網路上學來的
.Document.Focus
為什麼要這句呢?有什麼差別?

因該沒差別的,因程式有時會出錯,加上後很流暢.
右邊的是frame為什麼也是all(0)而不是all(1)

11# 有註明   all(6):排行第6位(下限索引值從0 開始)的[元素];.all(0)排行第0的[元素]
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 18# GBKEE

謝謝超版解決這個問題!雖然我不是用這個方法抓的!不過還是要感謝

再請問超版

.Document.Focus

為什麼要這句呢?有什麼差別?

.all(5)的5我是一個一個試出來的

左邊的是frame是all(0)
右邊的是frame為什麼也是all(0)而不是all(1)
超版也是一個一個試出來的嗎?

還是有別的語法可以list出來呢?

感謝超版又學到東西

TOP

回復 16# GBKEE

我指的是「區域變數視窗」

請問版主還有更好的查找方法嗎?

TOP

本帖最後由 GBKEE 於 2014-8-8 09:51 編輯

回復 17# ciboybj


   
有些查詢資料(例如查詢"電腦")會有下一頁的問題

哪要先看這網頁的原始文件 [下一頁]鍵的名稱.
  1. Sub 日報表()
  2.     Dim D  As Object, E As Object, P As Integer, URL As String, 頁數 As Integer, i As Integer
  3.     Dim P_down As Object
  4.     URL = "http://bsr.twse.com.tw/bshtm/"
  5.     With CreateObject("InternetExplorer.Application")
  6.         .Navigate URL
  7.         .Visible = True
  8.          Do While .readyState <> 4 Or .Busy
  9.             DoEvents
  10.         Loop
  11.         Set D = .Document.all(5).all(0).contentWindow.frames.Document.getelementsbytagname("INPUT")
  12.         
  13.         D("txtTASKNO").Value = "1101"
  14.         For Each E In D
  15.             If E.Value = "查詢" Then E.Click: Exit For
  16.         Next
  17.         Do While .readyState <> 4 Or .Busy
  18.             DoEvents
  19.         Loop
  20.         頁數 = .Document.all(5).all(0).contentWindow.frames.Document.getElementsByName("sp_ListCount")(0).INNERTEXT
  21.          For Each E In .Document.all(5).all(0).contentWindow.frames.Document.getelementsbytagname("INPUT")
  22.                 If E.Value = "下一頁" Then Set P_down = E: Exit For
  23.             Next
  24.          ActiveSheet.Cells.Clear
  25.          .Document.Focus
  26.         For P = 1 To 頁數
  27.             Set D = Nothing
  28.             Do
  29.                 DoEvents
  30.                 Set D = .Document.all(5).all(1).contentWindow.frames.Document.getelementsbytagname("table") '
  31.                
  32.             Loop Until Not D Is Nothing And D.Length = 7
  33.             For i = IIf(P = 1, 3, 4) To 4
  34.                 Ep D(i).outerHTML
  35.             Next
  36.             P_down.Click
  37.             Do While .readyState <> 4 Or .Busy
  38.                 DoEvents
  39.             Loop
  40.         Next
  41.         .Quit
  42.     End With
  43. End Sub
  44. Private Sub Ep(s As String) ' A(A.Length - 1).outerHTML
  45.     With CreateObject("InternetExplorer.Application")
  46.         .Navigate "about:Tabs"
  47.         .Visible = True
  48.         .Document.body.innerHTML = s
  49.         .ExecWB 17, 2       '  Select All
  50.         .ExecWB 12, 2       '  Copy selection
  51.         With ActiveSheet
  52.           '  MsgBox .UsedRange.Rows.Count
  53.             .Range("A" & IIf(.UsedRange.Rows.Count = 1, 1, .UsedRange.Rows.Count + 1)).Select
  54.             .PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False, NoHTMLFormatting:=True
  55.         End With
  56.         .Quit
  57.     End With
  58. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

GBKEE 您好
真的很謝謝您
另外
想請問您 有些查詢資料(例如查詢"電腦")會有下一頁的問題
想請教您 此種狀況該如何解決
謝謝您

TOP

回復 15# joey0415


   
vba內建的資料字型不能放大

是這個嗎?

感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 【時間如鑽石】時間對一個有智慧的人而言,就如鑽石般珍貴;但對愚人來說,卻像是一把泥土,一點價值也沒有。
返回列表 上一主題