【问题标题】:Cannot extract data from a span itemprop无法从 span itemprop 中提取数据
【发布时间】:2018-12-10 07:31:36
【问题描述】:

我有以下内容可以从网页中提取一些价格和可用性。但我在:

设置价格 = ie.Document.querySelector(".price-cont .final-price")

为什么?

Sub getMetaDataInfo()
Dim ie As New InternetExplorer
Dim mylink As String
Dim wb As Workbook: Set wb = ThisWorkbook
Dim wks As Worksheet
Dim lastrow As Integer
Set wks = wb.Sheets("Info")
Dim i As Integer
lastrow = wks.Cells(Rows.Count, "B").End(xlUp).Row

For i = 2 To lastrow

mylink = wks.Cells([i], 2).Value   

ie.Visible = False
ie.Navigate mylink

Do
DoEvents
Loop Until ie.ReadyState = READYSTATE_COMPLETE

Dim price As Object, availability As Object

Set price = ie.Document.querySelector(".price-cont .price")
wks.Cells(i, "C").Value = price.innerText   

Set availability = ie.Document.querySelector(".inner-box-one .availability")
wks.Cells(i, "D").Value = availability.innerText   

Next i

End Sub

我尝试像下面这样插入延迟

Sub getMetaDataInfo()
Dim IE As New InternetExplorer

Dim mylink As String
Dim wb As Workbook: Set wb = ThisWorkbook
Dim wks As Worksheet
Dim lastrow As Integer
Set wks = wb.Sheets("Info")
Dim i As Integer

lastrow = wks.Cells(Rows.Count, "B").End(xlUp).Row

IE.Visible = True


For i = 2 To lastrow

mylink = wks.Cells(i, 2).Value

IE.Visible = False
IE.Navigate mylink


Dim price As Object, t As Date
Const MAX_WAIT_SEC As Long = 5

Dim price As Object, availability As Object

While IE.Busy Or IE.ReadyState < 4: DoEvents: Wend
t = Timer
Do
    DoEvents
    On Error Resume Next

    Set price = IE.Document.querySelector(".price-cont .final-price")
    wks.Cells(i, "C").Value = price.innerText

    If Timer - t > MAX_WAIT_SEC Then Exit Do
    On Error GoTo 0
Loop
If price Is Nothing Then Exit Sub


Next i

End Sub

我的情况是我先手动登录网页我保持 IE 窗口打开我去 excel 运行宏但是..

【问题讨论】:

  • 不知道网页源码很难回答,请分享网址
  • 您还需要一个适当的等待(当 ie.Busy 或 ie.readyState
  • 它只工作了一两次,这让我想知道为什么??!它是我首先登录的产品页面
  • 您没有足够的时间。请参阅下面的答案。

标签: html excel vba web-scraping


【解决方案1】:

不看 HTML/URL 就很难判断。您是否验证了选择器是否正确?

否则,您现在可以做的主要两件事是:让页面有足够的时间加载:

1) 在尝试选择之前添加适当的等待

While ie.Busy Or ie.readyState < 4: DoEvents: Wend

2) 尝试定时循环以允许更多的加载时间

Option Explicit
Public Sub LoopUntilSet()
    Dim price As Object, t As Date
    Const MAX_WAIT_SEC As Long = 5

    'your other code

    While ie.Busy Or ie.readyState < 4: DoEvents: Wend
    t = Timer
    Do
        DoEvents
        On Error Resume Next
        Set price = ie.document.querySelector(".price-cont .price")
        If Timer - t > MAX_WAIT_SEC Then Exit Do
        On Error GoTo 0
    Loop
    If price Is Nothing Then Exit Sub

    'other code.....
End Sub

3) 去掉i周围的[]

【讨论】:

  • 更改后我收到一个奇怪的 VBA 自动化错误 -2147467259 (800040005)
  • 在 ie.Visible = False 但即使更改为 true 也一样
  • 这不是我的代码造成的。你能pastebin你当前的代码包括我的行吗?
  • 看起来很奇怪。尝试创建一个 IE 对象,而不是使用 Dim ie as New Internet Explorer 而不是 Dim ie As InternetExplorer 自动实例化:设置 ie = New InternetExplorer……另外,尝试使用 InternetExplorerMedium 代替 InternetExplorer。
  • 我做到了,但步骤不同。因为是登录页面,所以我选择了visible true with closed IE window。代码运行后,它会打开我登录的窗口,然后它会从一个产品转到另一个产品,它正在工作..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-06
  • 2019-08-27
相关资源
最近更新 更多