返回列表 上一主題 發帖

[發問] 查找網站資料

回復 5# ciboybj
試試看
  1. Option Explicit
  2. Sub Ex()
  3.     Dim D  As Object, e As Object, URL As String
  4.     '工業用酵素
  5.     '工業用牛奶發酵劑
  6.     '液晶
  7.     '防腐劑
  8.     '工業用除臭劑
  9.     '工業用乾燥劑
  10.     '中和劑
  11.     URL = "http://tmsearch.tipo.gov.tw/TIPO_DR/GoodsIPOContent.jsp"
  12.     With CreateObject("InternetExplorer.Application")
  13.         .Navigate URL
  14.         .Visible = True
  15.          Do While .ReadyState <> 4 Or .Busy
  16.             DoEvents
  17.         Loop
  18.         Set D = .document.all(6).all(0).contentWindow.frames.document.getElementsByTagName("INPUT")
  19.         D("txtGoodsName").Value = "中和劑"
  20.         For Each e In D
  21.             If e.Value = "查詢" Then e.Click: Exit For
  22.         Next
  23.         Do While .ReadyState <> 4 Or .Busy
  24.             DoEvents
  25.         Loop
  26.         Set D = .document.LastChild.LastChild.all(1).contentWindow.frames.document.getElementsByTagName("table")
  27.         Ep D(0).outerHTML
  28.         .Quit
  29.     End With
  30. End Sub
  31. Private Sub Ep(S As String)
  32.     With CreateObject("InternetExplorer.Application")
  33.         .Navigate "about:Tabs"
  34.         .Visible = True
  35.        .document.body.innerhtml = S
  36.         .ExecWB 17, 2       '  Select All
  37.         .ExecWB 12, 2       '  Copy selection
  38.         With ActiveSheet
  39.             .Cells.Clear
  40.             .Range("A1").Select
  41.             .PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False, NoHTMLFormatting:=True
  42.         End With
  43.         .Quit
  44.     End With
  45. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 8# ciboybj
物件不支援此屬性或方法 (錯誤 438)
不好意思我只有2003版.無法替你修正

請修改6#的程式碼
   
  1. Dim IE As Object
  2.     Set IE = CreateObject("InternetExplorer.Application")
  3.     With IE
  4.     'With CreateObject("InternetExplorer.Application")
複製代碼

有錯誤時 請到如圖 IE物件找找 "table" 在哪裡


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

TOP

回復 8# ciboybj
找了一下2010版 試試看可改成如下
  1. ' Set D = .document.LastChild.LastChild.all(1).contentWindow.frames.document.getElementsByTagName("table")
  2.         Set D = .document.all(6).all(1).contentWindow.frames.document.getElementsByTagName("table")
複製代碼
回復 10# joey0415
Set D = .document.all(6).all(0).contentWindow.frames.document.getElementsByTagName("INPUT")
解釋如下
.document->HTML的[文件物件]
.All->document的element [元素集合]
all(6):排行第6位(下限索引值從0 開始)的[元素];.all(0)排行第0的[元素];  
contentWindow屬性是指指定的frame或者iframe所在的window物件
getElementsByTagName("INPUT")->元素中的TagName為"INPUT"的集合物件
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 13# joey0415


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

TOP

回復 15# joey0415


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

是這個嗎?

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

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

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

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

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

回復 25# cadillac
  1. Private Function 報表頁數(股票代號 As String)   '參數傳送來的 --股票代號
  2.     With CreateObject("InternetExplorer.Application")
  3.             .Navigate "http://bsr.twse.com.tw/bshtm/bsMenu.aspx"
  4.             Do While .Busy Or .readyState <> 4
  5.                 DoEvents
  6.             Loop
  7.             '這裡沒錯誤 *****
  8.             .document.getElementsByName("txtTASKNO").Item(0).Value = 股票代號         
  9.          .document.getElementsByName("btnOK")(0).Click    '這裡為何沒錯誤???******
  10.             Do While .Busy Or .readyState <> 4
  11.                 DoEvents
  12.             Loop
  13.             '可以自己 改一改***********
  14.             報表頁數 = .document.getElementsByName("sp_ListCount")(0).innertext
  15.             .Quit
  16.     End With
  17. End Function
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

本帖最後由 GBKEE 於 2014-8-15 06:35 編輯

回復 28# cadillac
網頁設有[認證碼],限制用程式下載.功力不夠破解不了.

你有查出網頁原始碼<span id="sp_ListCount">15</span>
試試看
  1. .document.getElementByID("sp_ListCount").INNERTEXT            
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 【時日莫空過】一個人在世間做了多少事,就等於壽命有多長。因此必須與時間競爭,切莫使時日空過。
返回列表 上一主題