Ok so here is my entire code:
Private Sub CommandButton1_Click()
Dim appIE As Object
Set appIE = CreateObject("internetexplorer.application")
With appIE
.Navigate "http://finance.yahoo.com/q/ks?s=" & "AAPL"
.Visible = True
End With
Do While appIE.Busy
DoEvents
Loop
Set getPrice = appIE.Document.getElementById("yfs_l84_aapl")
Dim myValue As String: myValue = getPrice.Cells(1).innerHTML
appIE.Quit
Set appIE = Nothing
Range("B1").Value = myValue
End Sub
And here is the HTML that I'm trying to read into Excel (specifically, I need the 113.92):
<span id="yfs_l84_aapl">113.92</span>
What do I have to change in these two lines of code to read a "span id"?
Set getPrice = appIE.Document.getElementById("yfs_l84_aapl")
Dim myValue As String: myValue = getPrice.Cells(1).innerHTML
Or, alternatively, is there a way just to read whatever is directly after "yfs_184"??
I'm brand new to coding and am working very hard to get better, so any help is really appreciated!! Thanks! :)
Use this:
myValue = getPrice.innerText
Related
Hello I have some code below that scrape's data off a website.
What I am struggling with is the Element id doesn't always exist on this website (which is fine) - but if this is the case, I want the value just to be returned as £0.
Instead I get 'Run-time error '424' Object required.
Which is because my ID "X123" isn't on the website.
Any help would be much appreciated.
Option Explicit
Sub getdata()
Dim wb As Object
Dim i As Integer
Dim sURL As String
Dim getprice As Object
Dim myValue As String
For i = 8 To Sheets("Sheet1").Range("B34").Value
Set wb = CreateObject("internetExplorer.Application")
sURL = Cells(i, 1)
wb.Navigate sURL
wb.Visible = False
Do While wb.Busy = True Or wb.ReadyState <> 4: DoEvents: Loop
Set getprice = wb.Document.getElementById("X123")
myValue = getprice.innerText
Sheets("Sheet1").Cells(i, 2).Value = myValue
wb.Quit
Set wb = Nothing
Next i
End Sub
Add some error handling
On Error Resume Next
Set getprice = wb.Document.getElementById("X123")
On Error GoTo 0
If getPrice Is Nothing Then
myValue = "£0" '<=Assuming £ is included and not formatted in sheet
Else
myValue = getprice.innerText
End If
I'm trying to retrieve values from external websites by element ID using VBA and add them to my excel table. The website URL's are indicated in column A. Column B and C are for my retrieved values.
URL example
Element ID name: "youtube-user-page-country"
Excel Pic
Bellow is my poor attempt:
Sub getCountry()
Dim IE As New InternetExplorer
IE.Visible = False
IE.navigate Worksheets("Sheet1").Range(A3).Value
Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Dim Doc As HTMLDocument
Set Doc = IE.document
Dim getCountry As String
getCountry = Trim(Doc.getElementsByTagName("youtube-user-page-country").innerText)
Worksheets("Sheet1").Range(B31).Value = getCountry
End Sub
The code isn't working showing problems with object definition.
Could anyone give me tips on where I'm going wrong?
I've been a macro recorder user and the switch has quite a steep learning curve :-)
Thanks for any help !
I think I get what you are after. There were a few issues:
You want to use getElementByID.
Naming a string getCountry and the SubRoutine getCountry containing it is not a good idea. You can do it, but don't.
Always fully qualify your sheet references so you know what workbook and sheet you are working with
Here's the revised code, I have it working on my end.
Sub getCountry()
Dim IE As Object: Set IE = CreateObject("InternetExplorer.Application")
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim Country As String
With IE
.Visible = False
.navigate ws.Range("A3").Value
Do
DoEvents
Loop Until .readyState = 4
End With
Country = Trim$(IE.document.getElementByID("youtube-user-page-country").innerText)
ws.Range("B31").Value2 = Country
IE.Quit
End Sub
You can use this to dump the data to your spreadsheet.
Sub DumpData()
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
URL = "http://finance.yahoo.com/q?s=sbux&ql=1"
'Wait for site to fully load
IE.Navigate2 URL
Do While IE.Busy = True
DoEvents
Loop
RowCount = 1
With Sheets("Sheet1")
.Cells.ClearContents
RowCount = 1
For Each itm In IE.document.all
.Range("A" & RowCount) = itm.tagname
.Range("B" & RowCount) = itm.ID
.Range("C" & RowCount) = itm.classname
.Range("D" & RowCount) = Left(itm.innertext, 1024)
RowCount = RowCount + 1
Next itm
End With
End Sub
Thanks Joel!!!
I'm trying to get month-to-date and year-to-date return values from the website
http://us.spindices.com/indices/equity/sp-oil-gas-exploration-production-select-industry-index
into an Excel spreadsheet using VBA. The problem is that there is no "id= " in the code of the page, which I understand would make this process a lot simpler. There is also the matter of which time period (year-to-date or month-to-date) is visible, but I'd be happy with scraping just the MTD values for now.
Here is my code:
Sub Get_Change()
'attempting to scrape Barclay's website
Dim appIE As Object
Dim MyVar As String
Set appIE = CreateObject("internetexplorer.application")
With appIE
.Navigate "http://us.spindices.com/indices/equity/sp-oil-gas-exploration-production-select-industry-index"
.Visible = True
End With
Do While appIE.Busy
DoEvents
Range("A1").Value = "Working..."
Loop
Set TDelements = appIE.document.getElementsbyClassName("performance-chart-table")
For Each TDelement In TDelements
If TDelement.class = "change" Then
MyVar = TDelement.class.innerText("Value")
End If
Next
Range("A1").Value = MyVar
appIE.Quit
Set appIE = Nothing
End Sub
If I can get a way to set the 'MyVar' variable to the current MTD or YTD value, I'll be done, but I'm having a hard time since there is not a unique identifier for either of these values. Any ideas?
I've recently watched some CSS training videos and I can tell you the CSS selector syntax is powerful and I'd recommend it. This is the same syntax that javascript/web developers use to select elements when using JQuery.
I think you should try using
document.queryselectorall
or in your case because you have drilled in to the document to get the "performance-chart-table" call queryselectorall off of that variable, TDelements.
Documentation at http://www.w3schools.com/jsref/met_document_queryselectorall.asp
and you supply as a parameter a CSS selector string the syntax of which can be found at http://www.w3schools.com/cssref/css_selectors.asp
And I've gone and done it for you....
Sub Get_Change()
'* Tools-References Microsoft HTML Object Library
'attempting to scrape Barclay's website
Dim appIE As Object
Dim MyVar As String
Set appIE = CreateObject("internetexplorer.application")
With appIE
.Navigate "http://us.spindices.com/indices/equity/sp-oil-gas-exploration-production-select-industry-index"
.Visible = True
End With
Do While appIE.Busy
DoEvents
Range("A1").Value = "Working..."
Loop
Dim htmlDoc As MSHTML.HTMLDocument
Set htmlDoc = appIE.document
Dim TDelements2 As MSHTML.IHTMLElementCollection
Set TDelements2 = htmlDoc.getElementsByClassName("performance-chart-table")
While TDelements2.Length < 1
DoEvents
Application.Wait (Now() + TimeSerial(0, 0, 3))
Set TDelements2 = htmlDoc.getElementsByClassName("performance-chart-table")
Wend
Dim oHTMLTablePerformanceChartTable As MSHTML.HTMLTable
Set oHTMLTablePerformanceChartTable = TDelements2.Item(0)
Dim objChangeCollection As MSHTML.IHTMLDOMChildrenCollection
Set objChangeCollection = oHTMLTablePerformanceChartTable.querySelectorAll(".change")
'Debug.Assert objChangeCollection.Length = 2
Dim objChange2 As Object
Set objChange2 = objChangeCollection.Item(1)
MyVar = objChange2.innerText
'Set TDelements = appIE.document.getElementsByClassName("performance-chart-table")
'
'For Each TDelement In TDelements
' TDelements.querySelectorAll (".change")
' If TDelement.class = "change" Then
' MyVar = TDelement.class.innerText("Value")
'
' End If
'Next
Range("A1").Value = MyVar
appIE.Quit
Set appIE = Nothing
End Sub
I am currently trying to scrap data from a website using VBA. I am following this tutorial and hence my code is the following one:
Sub Foo()
Dim appIE As Object
Set appIE = CreateObject("internetexplorer.application")
With appIE
.Navigate "https://www.ishares.com/it/investitore-privato/it/prodotti/251843/ishares-euro-high-yield-corporate-bond-ucits-etf"
.Visible = True
End With
Do While appIE.Busy
DoEvents
Loop
Set allRowOfData = appIE.document.getElementsByClassName("visible-data totalNetAssets")
Dim myValue As String
myValue = allRowOfData.Cells(1).innerHTML
MsgBox myValue
End Sub
Unfortunately there are some differences between data I want to scrap and those ones used in the example: this line
myValue = allRowOfData.Cells(1).innerHTML
is wrong according to VBA debug.
Anyone could provide me with some explanations about why that doesn't work and how am I supposed to pick the right method to scrap HTML pages?
Try the below change which will solve your issue. In brief, you will need to treat the allRowofData as a collection.
myValue = allRowOfData(0).Cells(1).innerHTML
Given an url, how can I get the title of the html page in VBA in Excel?
For example suppose I have three urls like :
http://url1.com/somepage.html
http://url2.com/page.html
http://url3.com/page.html
Now I need to get the title of these html pages in another column. How do I do it?
Remou's answer was VERY helpful for me, but it caused a problem: It doesn't close the Internet Explorer process, so since I needed to run this dozens of times I ended up with too many IEs open, and my computer couldn't handle this.
So just add
wb.Quit
and everything will be fine.
This is the code that works for me:
Function GetTitleFromURL(sURL As String)
Dim wb As Object
Dim doc As Object
Set wb = CreateObject("InternetExplorer.Application")
wb.Navigate sURL
While wb.Busy
DoEvents
Wend
GetTitleFromURL = wb.Document.Title
wb.Quit
Set wb = Nothing
End Function
I am not sure what you mean by title, but here is an idea:
Dim wb As Object
Dim doc As Object
Dim sURL As String
Set wb = CreateObject("InternetExplorer.Application")
sURL = "http://lessthandot.com"
wb.Navigate sURL
While wb.Busy
DoEvents
Wend
''HTML Document
Set doc = wb.document
''Title
Debug.Print doc.Title
Set wb = Nothing
if you use Selenium :
Sub Get_Title()
Dim driver As New WebDriver
debug.print driver.Title
End Sub