Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88087

Cannot extract data from a span itemprop

$
0
0

I have the following to extract some prices and availabilities from a webpage. But I get Object Required at:

Set price = ie.Document.querySelector(".price-cont .final-price")

Why?

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

I tried to insert the delay like the following

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

My scenario is I login first to webpage manually I keep the IE window open I go to excel run macro but..


Viewing all articles
Browse latest Browse all 88087

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>