使用 Excell Vba 浏览网站

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2632306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:23:08  来源:igfitidea点击:

Navigate a website with Excell Vba

excel-vbavbaexcel

提问by Ommit

I can pull data from known ulr, but can I navigate a website with excel.

我可以从已知的 ulr 中提取数据,但我可以使用 excel 浏览网站。

For example can excel do a google search and put the results on a spreadsheet. Or navigate an in-frame site.

例如可以 excel 进行谷歌搜索并将结果放在电子表格中。或者浏览框架内的网站。

This code pulls data from a website.

此代码从网站中提取数据。

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

回答by Ommit

Hey so I figure this one out and thought I'd post in case anyone had a similar problem.

嘿,所以我想出了这个,并认为我会发布以防万一有人遇到类似的问题。

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

So to do this successfully you'll need to know element names (you can do this by looking at the source code or installing an addin that helps you like firebug) you can also use .getElementsByID("someIDhere").

因此,要成功执行此操作,您需要知道元素名称(您可以通过查看源代码或安装可帮助您喜欢 firebug 的插件来完成此操作),您还可以使用 .getElementsByID("someIDhere")。

回答by Charles Byrne

I know it's a little late, but here's a solution. You'll need to do the interface to pass in the search term, validate/encode it if needed, etc. It requires Internet Explorer on your PC. It will give you the raw HTML and you'll have to parse it as needed.

我知道现在有点晚了,但这里有一个解决方案。你需要做接口来传递搜索词,如果需要,验证/编码它,等等。它需要你的 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