【问题标题】:Scraping on Amazon Prime Now postcode entry抓取 Amazon Prime Now 邮政编码条目
【发布时间】:2015-10-15 11:23:10
【问题描述】:

我希望在 primenow.amazon.co.uk 上输入邮政编码提交表单并将结果粘贴到 excel 中

Sub PostCode_Delivery_Short()

Dim ie As Object
Dim form As Variant, button As Variant

'add the “Microsoft Internet Controls” reference in your VBA Project indirectly
Set ie = CreateObject("InternetExplorer.Application")
'Input box for postcode
    Postcode = InputBox("Enter Postcode")

With ie
    .Visible = True
    .Navigate ("primenow.amazon.co.uk")

'we ensure that the web page downloads completely before we fill the form automatically
Application.Wait (Now + TimeValue("00:00:04"))

'assigning the vinput variables to the html elements of the form
ie.Document.getElementsByClassName("availability__form__instructions__heading")(0).innertext = Postcode

'accessing the button via the form
Set form = ie.Document.getElementsByClassName("form")

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

End With

'cleaning up memory
Set ie = Nothing
End Sub

我正在努力解决的是元素 ID(我认为),我不断收到运行时“对象变量错误或未设置块变量”。

【问题讨论】:

    标签: excel vba forms web-scraping screen-scraping


    【解决方案1】:

    getElementsByName 返回一个集合,而不是单个元素,因此请尝试以下操作:

    ie.Document.getElementsByName("prime-now-input")(0).innertext = Postcode
    

    编辑另外:

    .Document.getElementsByTagName("availability__form__instructions__heading")
    

    我很确定没有带有该标签名称的 HTML 元素 ;-) 也许你的意思是getElementsByClassName()

    EDIT2:这是您需要输入邮政编码的元素(使用 .Value,而不是 .innerText)

    <input type="text" 
    placeholder="Enter a postcode to see if we deliver to your area ..." 
    maxlength="9" data-reactid=".0.1.0.1.1.0.0.1.0.0">
    

    我的 IE 版本甚至不渲染输入,所以我无法提供更多建议。

    【讨论】:

    • 不,没有让它工作,不过感谢您的帮助。
    • 又试了一次,上课了,但没有运气...我已经把vba删了,看看,如果你不介意
    • 您将通过 1) 更新您的问题以显示您当前的代码和 2) 准确指出哪些行触发了错误,从而增加解决此问题的机会。
    • 到达这一行 ie.Document.getElementsByClassName("availability__form__instructions__heading")(0).innertext = Postcode
    【解决方案2】:

    该网站可能有更新,以下是针对英国的,但它输入邮政编码并按下Shop Now(提交)按钮。

    我使用input[type='submit'] 的CSS 选择器来定位提交按钮。它读取为带有input 标签的元素,该标签具有type 属性,值为'submit'。 “[]”表示属性。使用querySelector 方法应用此选择器将根据需要仅检索第一个匹配项。

    Option Explicit
    Public Sub SubmitPostCode()
        Dim ie As New InternetExplorer
        Const URL As String = "https://primenow.amazon.co.uk/onboard?sourceUrl=%2F"
        Const POSTCODE As String = "WC1A 1DG"
        With ie
            .Visible = True
            .navigate URL
            While .Busy Or .readyState < 4: DoEvents: Wend
                .document.getElementById("lsPostalCode").Value = POSTCODE
                .document.querySelector("input[type='submit']").Click
            While .Busy Or .readyState < 4: DoEvents: Wend
            Stop '<== Delete me
            .Quit
        End With
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-11
      • 2020-01-24
      • 2020-04-21
      • 1970-01-01
      • 1970-01-01
      • 2013-04-05
      • 1970-01-01
      相关资源
      最近更新 更多