如何使用excel vba从网站上提取价格?

时间:2019-07-04 08:08:25

标签: excel vba web-scraping

html

    <tr>
    <td class = "pricing-description">Price:</td>
    <td class = "price base-price">
    <span>$</span>
    <span> 4.99 </span>

并且我尝试使用此代码来获取Excel中的值:

myValue = ie.document.querySelector(".price base-price[Price:]").innerText
Worksheets("Sheet1").Range("A1").Value = myValue

Dim erow As Long
Dim ele As Object

Set sht = Sheets("Sheet1")

erow = Sheet1.Cells(rows.Count, 1).End(xlUp).Offset(1, 0).Row

Dim ws As Worksheet

Set ws = ActiveSheet
Set objIE = CreateObject("Internetexplorer.application")

searchterm = InputBox("ENTER CARTER'S SEARCH TERM")

With objIE
    .Visible = True
    .navigate "www.autozone.com"

    Do While .Busy Or _
       .readyState <> 4
        DoEvents
    Loop

    .document.getElementsByClassName("hdinput typeAhead topSearch").item.innerText = searchterm

    .document.querySelector("input[type=submit]").Click
    Do While .readyState <> READYSTATE_COMPLETE
        DoEvents
    Loop


    myValue = ie.document.querySelector(".price base-price[Price:]").innerText
    Worksheets("Sheet1").Range("A1").Value = myValue

我的预期结果是从excel网站上获取价格,一旦开始工作,我就可以在很多地方做到这一点。

1 个答案:

答案 0 :(得分:1)

myValue = IE.document.querySelector(".price base-price[Price:]").innerText

试图用节点的innerText值(先前的同级td)替换attribute selector中的属性名称,以及通常用来表示伪的字符(:)。另外,您缺少class selectorbase-price搭配使用,即.base-price,否则它将变成type selector;并且,当我们看到td元素是同级元素时,您通过在两个类名称之间使用descendant combinator来指定父子关系。

这是您的选择者所说的:

点击放大

enter image description here

根据其外观,您只需要该节点的直接类,然后访问.innerText

myValue = IE.document.querySelector(".base-price").innerText

其他信息:

  1. Pseudo-classes and pseudo-elements