返回列表 上一主題 發帖

Excel VBA 抓HTML 中的某一個Table

回復 1# BarryHuang621a
你提供智慧選股工具的網址,在我的  XP, IE8  環境下執行很慢,有時會掛掉

請試試別的網址
  1. Option Explicit
  2. Sub Ex()
  3.     Dim D  As Object, i As Integer, URL As String
  4.     URL = "http://forum.twbts.com/thread-12273-1-2.html"
  5.     'URL = "http://www.goodinfo.tw/stockinfo/StockSorter.asp?"
  6.     With CreateObject("InternetExplorer.Application")
  7.         .Navigate URL
  8.         .Visible = True
  9.          Do While .ReadyState <> 4 Or .Busy
  10.             DoEvents
  11.         Loop
  12.       
  13.         Set D = .document.getElementsByTagName("table")
  14.         ActiveSheet.Cells.Clear
  15.         For i = 0 To D.Length - 1
  16.             Ep i, D(i).outerHTML
  17.         Next
  18.         .Quit
  19.     End With
  20. End Sub
  21. Private Sub Ep(i As Integer, S As String)
  22.     Dim R
  23.     With CreateObject("InternetExplorer.Application")
  24.         .Navigate "about:Tabs"
  25.         .Visible = True
  26.         .document.body.innerhtml = S
  27.         .ExecWB 17, 2       '  Select All
  28.         .ExecWB 12, 2       '  Copy selection
  29.         With ActiveSheet
  30.             R = IIf(.UsedRange.Rows.Count = 1, 1, .UsedRange.Rows.Count + 2)
  31.             .Cells(R, 1) = "第 " & i & " 個 Table"
  32.             .Cells(R, 1).EntireRow.Interior.Color = vbYellow
  33.             .UsedRange.Cells(R + 1, 1).Select
  34.             .PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False, NoHTMLFormatting:=True
  35.         End With
  36.         .Quit
  37.     End With
  38. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

本帖最後由 GBKEE 於 2014-8-11 08:34 編輯

回復 8# BarryHuang621a
  1. Excel 外部資料
  2. VBA 抓取網頁內的某一個Table
  3. Excel VBA 抓HTML 中的某一個Table
複製代碼
你發了這些有相關的問題,請不需一直的發新主題
幫你整理一下,關於主題中的回覆,是否你要表達明白與否,不然回覆者會覺的是剃頭擔子一頭熱.


Excel VBA 讀取網業存檔
試試看是這樣嗎?
  1. Option Explicit
  2. Private Sub Ex()
  3.     Dim xml As Object     '用來取得網頁資料
  4.     Dim stream            'As ADODB.stream   '用來儲存二進位檔案
  5.     Dim URL As String     '目的網址
  6.     Dim Title As String
  7.     Set xml = CreateObject("Microsoft.XMLHTTP")
  8.     Set stream = CreateObject("ADODB.stream")
  9.     URL = "https://tw.stock.yahoo.com/q/q?s=2303"
  10.     xml.Open "POST", URL, 0
  11.     xml.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  12.     xml.send
  13.     Title = Split(xml.responseText, "<title>")(1)
  14.     Title = Split(Title, "</title>")(0) & ".HTM"   '指定網頁檔案名稱
  15.     Title = "D:\" & Title                          '網頁存檔位置
  16.     With stream
  17.         .Open
  18.         .Type = 1
  19.         .write xml.ResponseBody
  20.         If Dir(Title) <> "" Then Kill Title
  21.         .SaveToFile Title
  22.         .Close
  23.     End With
  24.     MsgBox "存檔於 " & Title
  25. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 【時間成就一切】時間可以造就人格,可以成就事業,也可以儲積功德。
返回列表 上一主題