2010-04-13 62 views
1

我可以從已知的ulr中提取數據,但是我可以使用excel瀏覽網站。使用Excell Vba導航網站

例如可以擅長做谷歌搜索並將結果放在電子表格上。或導航框架內網站。

此代碼從網站中提取數據。

With ActiveSheet.QueryTables.Add(Connection:= _ 
    PUT_URL_HERE, _ 
    Destination:=Range("A1")) 
    .Name = "I need serious help" 
    .FieldNames = True 
    .RowNumbers = False 
    .FillAdjacentFormulas = False 
    .PreserveFormatting = True 
    .RefreshOnFileOpen = False 
    .BackgroundQuery = True 
    .RefreshStyle = xlInsertDeleteCells 
    .SavePassword = False 
    .SaveData = True 
    .AdjustColumnWidth = False 
    .RefreshPeriod = 0 
    .WebSelectionType = xlAllTables 
    .WebFormatting = xlWebFormattingNone 
    .WebPreFormattedTextToColumns = True 
    .WebConsecutiveDelimitersAsOne = True 
    .WebSingleBlockTextImport = False 
    .WebDisableDateRecognition = False 
    .WebDisableRedirections = False 
    .Refresh BackgroundQuery:=False 
End With 
+0

創建一個宏來跟蹤我做一個網絡查詢並不能很好地破解它。 – Ommit 2010-04-13 21:01:38

+0

考慮導航基於HTML的網站涉及的內容:您需要發送包含適當數據的請求,然後閱讀返回的文檔。 Excel可以引用適當的庫,通過VBA – barrowc 2010-04-16 02:33:35

+0

來做這兩件事情,這是令人鼓舞的,要知道這是可能的。你知道任何好的資源,或者這個正在做的例子 – Ommit 2010-04-19 17:49:52

回答

4

嘿所以我想出了這一個,並認爲我會張貼以防萬一任何人有類似的問題。

Sub googlesearch() 
    Set objIE = CreateObject("InternetExplorer.Application") 
    WebSite = "www.google.com" 
    With objIE 
     .Visible = True 
     .navigate WebSite 
     Do While .Busy Or .readyState <> 4 
      DoEvents 
     Loop 

     Set Element = .document.getElementsByName("q") 
     Element.Item(0).Value = "Hello world" 
     .document.forms(0).submit 
     '.quit 
     End With 

End Sub 

因此,要成功地做到這一點,你需要知道的元素名稱(您可以通過查看源代碼或安裝插件,可以幫助你像螢火蟲這樣做),你也可以使用.getElementsByID(「someIDhere 「)。

0

我知道這有點晚了,但這裏有一個解決方案。您需要執行界面來傳遞搜索詞,驗證/編碼它,如果需要的話,等等。它需要您的PC上的Internet Explorer。它會給你原始的HTML,你必須根據需要解析它。

Function GoogleSearch(strSearchTerm As String) As String 
    Dim ie As Object 
    Dim sHTML As String 

    On Error GoTo ZERR 
    Set ie = CreateObject("InternetExplorer.Application") 

    With ie 
     .Visible = False 
     .navigate ("http://www.google.com/search?q=" & strSearchTerm) 
     Do Until .readystate = 4 
      DoEvents 
     Loop 
     sHTML = .document.Body.innerHTML 
    End With 
    Set ie = Nothing 
GoogleSearch = sHTML  
ZERR: 
    Set ie = Nothing 
End Function