Excel VBA - 从网页中提取数据

标签 excel vba internet-explorer web-scraping

我正在尝试通过自动化网络浏览器从亚马逊页面提取卖家信息和价格。我正在尝试运行以下代码,但收到的错误是:

Object Variable or With Block variable not set.

有人可以指导我哪里出错了。

Option Explicit
Sub RunNewModule()  
    Dim ie As InternetExplorer
    Dim html As HTMLDocument
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = False
    ie.Navigate "http://www.amazon.com/gp/offer-listing/B00SVA81Z2/ref=dp_olp_new_mbc?ie=UTF8&condition=new"
    Dim priceData As Variant
    Dim sellerdata As Variant
    Dim item As Variant
    Dim cntr As Integer
    priceData = html.getElementsByClassName("olpOfferPrice").getElementsByTagName("span")(0).innerText
    cntr = 1
    For Each item In priceData
        Range("B" & cntr) = item.innerText
        cntr = cntr + 1
    Next item
    sellerdata = html.getElementsByClassName("olpSellerName").getElementsByTagName("span")(0).innerText    
    cntr = 1
    For Each item In sellerdata
        Range("A" & cntr) = item.innerText
        cntr = cntr + 1
    Next item  
End Sub

最佳答案

您没有分配 html,它现在为空。

你应该这样分配它:

Set html= ie.Document

通过元素的类名获取元素:

Dim ie As InternetExplorer
Dim html As IHTMLDocument
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = False
ie.Navigate "http://stackoverflow.com/questions/34463544/vba-fetching-data-from-class-name"
While ie.Busy
    DoEvents
Wend
While ie.ReadyState < 4
    DoEvents
Wend
Set html = ie.Document
Dim elements As IHTMLElementCollection
Set elements = html.getElementsByClassName("question-hyperlink")
If elements.Length > 0 Then
    MsgBox elements(0).innerText
End If
ie.Quit
Set ie = Nothing

enter image description here

不要忘记添加引用:

  • Microsoft Internet 控件
  • Microsoft Html 对象库
<小时/>

对于该亚马逊链接:

Dim ie As InternetExplorer
Dim html As HTMLDocument
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = False
ie.Navigate "http://www.amazon.in/gp/offer-listing/B00EYCBGNA/ref=dp_olp_new_mbc?ie=UTF8&condition=new"
While ie.Busy
    DoEvents
Wend
While ie.ReadyState < 4
    DoEvents
Wend

Set html = ie.Document
Dim elements As IHTMLElementCollection
Set elements = html.getElementsByClassName("olpOfferPrice")
For i = 0 To elements.Length - 1
     Sheet1.Range("A" & (i + 1)) = elements(i).innerText
Next i

Set elements = html.getElementsByClassName("olpSellerName")
For i = 0 To elements.Length - 1
    Sheet1.Range("B" & (i + 1)) = elements(i).innerText
Next i
ie.Quit
Set ie = Nothing

enter image description here

关于Excel VBA - 从网页中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34463544/

相关文章:

vba - 无法使 CommandBar 可见

python - 防止 csvkit 修改日期/时间?

excel - VBA Excel 范围法

Excel 超链接类型的函数,带有单击事件来执行 POST

css - IE 11 中的 Peekaboo 错误(AdSense 在与窗口交互之前不显示)

css - Internet Explorer 11+ 不呈现文本溢出 : Elipses Properly With Font Awesome

excel - 拉取从单个单元格溢出的每第 n 组行

java - 为什么在获取 excel 单元格时会出现 nullpointerexception

vba - 初始化 VBA 类 - 寻找更优雅的解决方案

html - padding-top inside input field in ie 中的填充顶部