No error, but No Data resulting from VBA HTML Pull - html

EDITED: So this one works for me.. I am looking to convert this to my purposes now. I am trying to switch this to the title and price, but when I convert the line:
Set links = .document.querySelectorAll(".s-item__link[href]")
to
Set links = .document.querySelectorAll(".s-item__title [h3]")
It pulls "[object HTMLHeadingElement]" for each row - thoughts?
Option Explicit
Public Sub GetInfo()
Dim ie As New InternetExplorer, ws As Worksheet, t As Date
Const MAX_WAIT_SEC As Long = 10
Set ws = ThisWorkbook.Worksheets("Sheet1")
With ie
.Visible = True
.Navigate2 "https://www.ebay.com/"
While .Busy Or .readyState < 4: DoEvents: Wend
ie.document.querySelector("#gh-ac").Value = "Bike"
ie.document.querySelector("#gh-btn").Click
While .Busy Or .readyState < 4: DoEvents: Wend
Dim links As Object, i As Long, count As Long
t = Timer
Do
On Error Resume Next
Set links = .document.querySelectorAll(".s-item__link[href]")
count = links.Length
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While count = 0
For i = 0 To links.Length - 1
ws.Cells(i + 1, 1) = links.item(i)
Next
.Quit
End With
End Sub

Here is an example of doing a search for Bike on ebay and looping until links are present
Option Explicit
Public Sub GetInfo()
Dim ie As New InternetExplorer, ws As Worksheet, t As Date
Const MAX_WAIT_SEC As Long = 10
Set ws = ThisWorkbook.Worksheets("Sheet1")
With ie
.Visible = True
.Navigate2 "https://www.ebay.com/"
While .Busy Or .readyState < 4: DoEvents: Wend
ie.document.querySelector("#gh-ac").Value = "Bike"
ie.document.querySelector("#gh-btn").Click
While .Busy Or .readyState < 4: DoEvents: Wend
Dim links As Object, i As Long, count As Long
t = Timer
Do
On Error Resume Next
Set links = .document.querySelectorAll(".s-item__link[href]")
count = links.Length
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While count = 0
For i = 0 To links.Length - 1
ws.Cells(i + 1, 1) = links.item(i)
Next
.Quit
End With
End Sub

Related

How to get data from class name inside a id name in excel VBA?

How can I get data from the classes date and number (all 3) data?
Sub Fii_dii()
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "https://www.nseindia.com/products/content/equities/equities/fii_dii_market_today.htm"
While IE.readyState <> 4
DoEvents
Wend
'Copy data
MsgBox IE.Document.all.Item("fiiTable").innerText
End Sub
Those values are retrieved by the browser dynamically from another URI you can find in the network tab of the browser when refreshing the page. I would simply issue an xmlhttp request (faster and no browser) and use css class selectors to match on the required nodes by class. Loop the returned nodeList and write out to Excel
Option Explicit
Public Sub ScrapeValues()
Dim html As HTMLDocument, values As Object, i As Long, ws As Worksheet
Set html = New HTMLDocument
Set ws = ThisWorkbook.Worksheets("Sheet1")
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", "https://www.nseindia.com/products/dynaContent/equities/equities/htms/fiiEQ.htm", False
.send
html.body.innerHTML = .responseText
End With
Set values = html.querySelectorAll(".date, .number")
For i = 0 To values.Length - 1
With ws
.Cells(i + 1, 1) = values.Item(i).innerText
End With
Next
End Sub
If you want to use IE you need to have a test for those elements to be present and a timed loop e.g.
Option Explicit
Public Sub ScrapeValues()
Dim ie As InternetExplorer, values As Object, i As Long, ws As Worksheet, t As Date
Const MAX_WAIT_SEC As Long = 5 '<==adjust time here
Set ie = New InternetExplorer
Set ws = ThisWorkbook.Worksheets("Sheet1")
With ie
.Visible = True
.Navigate2 "https://www.nseindia.com/products/content/equities/equities/fii_dii_market_today.htm"
While .Busy Or .readyState <> 4: DoEvents: Wend
t = Timer
Do
Set values = .document.querySelectorAll(".date, .number")
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While values.Length = 0
If values.Length > 0 Then
For i = 0 To values.Length - 1
With ws
.Cells(i + 1, 1) = values.Item(i).innerText
End With
Next
End If
.Quit
End With
End Sub
--
References (VBE>Tools>References):
Microsoft HTML Object Library
Microsoft Internet Controls

How to optimize excel VBA for clicking url

Run time error "70" while VBA is running.
Sometime the code runs smooth but sometime does not. Wondering if there is more reliable code for proceeding. It always stop in If link.innerHTML = "Balance Sheet" Then end if
Public Sub Get()
Dim ie As Object
Dim URL As String, link As Object, alllinks As Object
Dim eRowa As Long, eRowb As Long, eRowc As Long
Dim var As Object
Set var = ThisWorkbook.Worksheets("Sheet2").Cells(1, 1)
URL = "https://www.marketwatch.com/investing/stock/" & var & "/financials"
Set ie = CreateObject("internetexplorer.application")
With ie
.Visible = True
.navigate URL
While .Busy Or .readyState < 4: DoEvents: Wend
Set alllinks = ie.document.getElementsByTagName("a")
For Each link In alllinks
If link.innerHTML = "Balance Sheet" Then
link.Click
End If
Next link
While .Busy Or .readyState < 4: DoEvents: Wend
End With
Set ie = Nothing
End Sub
Expect smooth running without error 70
Use a timed loop to wait for presence of a tag. Use an attribute = value css selector with $ ends with operator for faster targeting of the appropriate element
Option Explicit
Public Sub GetInfo()
Dim ie As Object, url As String, link As Object
Dim var As Range, t As Date
Const MAX_WAIT_SEC As Long = 10
Set var = ThisWorkbook.Worksheets("Sheet2").Cells(1, 1)
url = "https://www.marketwatch.com/investing/stock/" & var.value & "/financials"
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.navigate2 url
While .Busy Or .readyState < 4: DoEvents: Wend
t = Timer
Do
On Error Resume Next
Set link = .document.querySelector("[href$='/balance-sheet']")
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While link Is Nothing
If link Is Nothing Then Exit Sub
link.Click
While .Busy Or .readyState < 4: DoEvents: Wend
Stop '<== Delete me later
.Quit
End With
End Sub

How to get results for web scraping for different cells with a different url?

I am setting up a spreadsheet that will get the NAV for specific index funds every day from the blackrock website. There are a number of these index funds however and the URL changes for each cell. I am unsure how to make my way down the spreadsheet table I have made and place the target value in another cell.
I have tried listing every possible URL one after another and it seems to work, however, I keep receiving a run-time error so I must assume there is a more efficient way of doing this.
Sub GetCurrentPrices()
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Sheet1")
Dim text As String
With CreateObject("internetexplorer.application")
.navigate "https://www.blackrock.com/uk/individual/products/xxxxxx/"
Do While .Busy And .readyState <> 4: DoEvents: Loop
text = .document.getElementsByClassName("header-nav-data")(0).innerText
.Quit
End With
ws.Cells(32, 1).Value = text
With CreateObject("internetexplorer.application")
.navigate "https://www.blackrock.com/uk/individual/products/yyyyyy/"
Do While .Busy And .readyState <> 4: DoEvents: Loop
text = .document.getElementsByClassName("header-nav-data")(0).innerText
.Quit
End With
ws.Cells(33, 1).Value = text
End Sub
Currently, this code is copied about 22 times for each different URL. I have also tried something like this:
Sub GetCurrentPrices2()
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Sheet1")
Dim text As String
Dim i As Integer
i=32
With CreateObject("internetexplorer.application")
.navigate "https://www.blackrock.com/uk/individual/products/" & (ws.Range("H2:H24").Value) & "/"
Do While .Busy And .readyState <> 4: DoEvents: Loop
text = .document.getElementsByClassName("header-nav-data")(0).innerText
.Quit
End With
Do Until i > 46
ws.Cells(i,1).Value = text
Loop
End Sub
Assuming that Column A contains the index fund of interest, and that the data starts at Row 2, your code can be re-written as follows...
Sub GetCurrentPrices()
Dim ws As Worksheet
Dim text As String
Dim lastRow As Long
Dim i As Long
Set ws = ThisWorkbook.Worksheets("Sheet1")
With ws
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
With CreateObject("InternetExplorer.Application")
For i = 2 To lastRow
.navigate "https://www.blackrock.com/uk/individual/products/" & ws.Cells(i, 1).Value & "/"
Do While .Busy And .readyState <> 4: DoEvents: Loop
text = .document.getElementsByClassName("header-nav-data")(0).innerText
ws.Cells(i, 2).Value = text
Next i
.Quit
End With
Set ws = Nothing
End Sub

Second drop down in HTML is not getting selected through VBA

My VBA code is not able to select the second drop down option once the first drop down is selected. Not sure why one dropdown is loading and second is not responding as per below code? Appreciate if you could help on fixing this. Regards
Dim IE As InternetExplorer
Dim HTMLDoc As HTMLDocument
Dim commodityStr As String
Dim commodityObj As HTMLObjectElement
Dim commodityCodes As IHTMLElementCollection
Dim codeCounter As Long
Dim EDateStr As String
Dim EDateObj As HTMLObjectElement
Dim EdateCodes As IHTMLElementCollection
Dim i As Integer
commodityStr = "MADHYA PRADESH"
EDateStr = "REWA"
Set IE = New InternetExplorer
With IE
.Visible = True
.navigate "http://hydro.imd.gov.in/hydrometweb/(S(ryta1dvaec5pg03bdnxa5545))/DistrictRaifall.aspx"
While .Busy Or .readyState <> READYSTATE_COMPLETE: Wend
Set HTMLDoc = IE.document
End With
Set commodityObj = HTMLDoc.getElementById("listItems")
For codeCounter = 0 To commodityObj.Length - 1
If commodityObj(codeCounter).innerText = commodityStr Then
commodityObj.Value = commodityObj(codeCounter).Value
commodityObj.Focus
commodityObj.FireEvent ("onchange")
While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE: Wend
Exit For
End If
Next
Set EDateObj = HTMLDoc.getElementById("DistrictDropDownList")
For codeCounter = 0 To EDateObj.Length - 1
If EDateObj(codeCounter).innerText = EDateStr Then
EDateObj.Value = EDateObj(codeCounter).Value
While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE: Wend
commodityObj.Focus
commodityObj.FireEvent ("onchange")
Exit For
End If
Next
You need a timed loop to allow for that execution of the onchange event. Also, you can make use of css attribute = value selectors to remove loops when targeting elements. It is likely the page does an XHR request so inspect via dev tools to see if that is the case.
Option Explicit
'VBE > Tools > References:
' Microsoft Internet Controls
Public Sub MakeSelections()
Dim ie As InternetExplorer, ele As Object, t As Date
Const MAX_WAIT_SEC As Long = 5
Dim commodity As String, iDate As String
commodity = "MADHYA PRADESH"
iDate = "REWA"
Set ie = New InternetExplorer
With ie
.Visible = True
.Navigate2 "http://hydro.imd.gov.in/hydrometweb/(S(3qitcijd521egpzhwqq3jk55))/DistrictRaifall.aspx"
While .Busy Or .readyState < 4: DoEvents: Wend
.document.querySelector("[value='" & commodity & "']").Selected = True
.document.querySelector("[name=listItems]").FireEvent "onchange"
t = Timer
Do
On Error Resume Next
Set ele = .document.querySelector("[value='" & iDate & "']")
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While ele Is Nothing
If Not ele Is Nothing Then
ele.Selected = True
.document.querySelector("#GoBtn").Click
Else
Exit Sub
End If
Stop
.Quit
End With
End Sub

scrape info from nested div excel vba

I'm trying to extract the start date from the following, but my code cant seem to see the 'nested" div "daysTips" any help would be great. Thank you.
HTML Screen Shot:
Sub warranty2()
Dim elements As IHTMLElementCollection
Dim IE As New InternetExplorer
Dim Doc As HTMLDocument
IE.Visible = True
IE.navigate "https://pcsupport.lenovo.com/gb/en/products/laptops-and-netbooks/thinkpad-x-series-laptops/thinkpad-x280-type-20kf-20ke/20ke/20kes5sd09/pc0x5yhz/warranty"
Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Set Doc = IE.document
Set elements = Doc.getElementsByClassName("daysTips")
For i = 0 To elements.Length - 1
Sheet1.Range("G" & (i + 1)) = elements(i).innerText
Next i
IE.Quit
End Sub
You need to do the click first on the down chevron
HTML:
Option Explicit
Public Sub GetInfo()
Dim IE As New InternetExplorer
With IE
.Visible = True
.navigate "https://pcsupport.lenovo.com/gb/en/products/laptops-and-netbooks/thinkpad-x-series-laptops/thinkpad-x280-type-20kf-20ke/20ke/20kes5sd09/pc0x5yhz/warranty"
While .Busy Or .readyState < 4: DoEvents: Wend
IE.document.querySelector(".icon.icon-s-down").Click
While .Busy Or .readyState < 4: DoEvents: Wend
Debug.Print IE.document.querySelector(".daysTips span").innerText
.Quit
End With
End Sub