使用VBA填写网站上的输入框

时间:2016-09-30 20:09:19

标签: html excel vba excel-vba web-scraping

我一直在撞墙,试图找出为什么这个VBA代码不起作用:(

我只是想将输入到excel输入框中的值插入到网站的输入框中。对于HTML,我是一个新手,所以我确信它与它有关。

以下是Zomato.com网站的HTML元素:

<input class="dark" id="location_input" role="combobox" aria-expanded="true" aria-labelledby="label_search_location" aria-owns="explore-location-suggest" aria-autocomplete="list" placeholder="Please type a location...">

这是我的VBA代码:

    Sub Merchant_Extraction()

Dim IE As Object
Dim form As Variant
Dim button As Variant

Set IE = CreateObject("internetexplorer.application")

merchantzip = InputBox("Enter Zip Code")

With IE

.Visible = True
.navigate ("http://www.zomato.com")

While IE.readystate <> 4
DoEvents
Wend

IE.Document.GetElementByID(“location_input_sp”).Item.innertext = merchantzip

Set form = IE.Document.getelementsbytagname("form")

Set button = form(0).onsubmit
form(0).get

End With

Set IE = Nothing

End Sub

我不清楚为什么它不起作用 - 任何帮助都会令人难以置信!

2 个答案:

答案 0 :(得分:1)

API XMLHTTP GET请求

提到了API。该文档是here

基本的免费帐户允许访问餐厅信息和搜索API(每天最多1000次通话)。

第一个0-20结果呼叫示例,其中指定了城市ID(英国曼彻斯特为68),如下所示;接收到JSON响应。它使用JSONConverter.bas

将响应解析为JSON对象
Option Explicit
Public Sub GetInfo()
    Dim URL As String, strJSON As String, json As Object

    URL = "https://developers.zomato.com/api/v2.1/search?entity_id=68&entity_type=city&start=0&count=20"

    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", URL, False
        .setRequestHeader "Content-Type", "application/json"
        .setRequestHeader "user-key", "yourAPIkey"
        .send
        strJSON = .responseText
    End With
    Set json = JsonConverter.ParseJson(strJSON)
    'other stuff with JSON object
End Sub

JSON响应示例:

enter image description here


Zomato-常用API:

API


查找城市ID:

对我来说,最快的方法是将城市连接到基本URL字符串,例如https://www.zomato.com/manchester,然后单击搜索,然后右键单击以检查第一个结果上的HTML。然后 Ctrl + F 弹出搜索框,搜索 CITY_ID ,然后浏览查找HTML的结果直到找到城市ID,例如

答案 1 :(得分:0)

只要在网页中输入值,正确的语法就是:

IE.Document.all.Item("location_input").Value = ""

我已将您的例程与我使用的一些代码结合起来,因此您可以看到一个示例。然而,我无法测试。在我的环境中,IE对象在.navigate部分之后断开连接,所以我添加了一个循环来查找并重新分配对象......

Option Explicit
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)


Sub Merchant_Extraction()
Dim IE As Object, objShellWindows As Object
Dim MerchantZip As String, strWebPath As String
Dim Form As Variant, Button As Variant
Dim X As Long

strWebPath = "http://www.zomato.com"

MerchantZip = InputBox("Enter Zip Code")
If MerchantZip = vbNullString Then Exit Sub

Set IE = CreateObject("InternetExplorer.Application")
With IE
    .Visible = True
    .Navigate strWebPath
End With

Do
    Sleep 250
    DoEvents
Loop While IE.Busy Or IE.ReadyState <> 4

If TypeName(IE) <> "IWebBrowser2" Or IE.Name <> "Internet Explorer" Then
    Set objShellWindows = CreateObject("Shell.Application").Windows
    For X = 0 To objShellWindows.Count - 1
        Set IE = objShellWindows.Item(X)
        If Not IE Is Nothing Then
            If IE.Name = "Internet Explorer" Then
                If InStr(1, IE.LocationURL, strWebPath, 1) > 0 Then
                    Do While IE.Busy Or IE.ReadyState <> 4
                        Sleep 250
                        DoEvents
                    Loop
                    Exit For
                End If
            End If
        End If
        Set IE = Nothing
    Next
    Set objShellWindows = Nothing
End If

If Not IE Is Nothing Then
    IE.Document.all.Item("location_input").Value = MerchantZip
    Sleep 250
    For Each Button In IE.Document.getelementsbytagname("form")
        If StrComp(Button.Type, "Button", 1) = 0 Then
            Button.Click
        End If
    Next
    Set IE = Nothing
End If

End Sub