Scraping data from website using vba 4 - vba

I am trying to pull minimum nighttime temperature for London from the Met office website using a VBA web scrape. I've tried to use code as posted here. While the code runs it isnt copying what i need it be copying. Assistance would be much appreciated.
Sub WebScrape_1()
'Create an Internet Explorer browser
Dim appIE As Object
Set appIE = CreateObject("internetexplorer.application")
'Browse the browser to the target webpage
With appIE
.Navigate "https://www.metoffice.gov.uk/public/weather/forecast/gcpvj0v07"
.Visible = True ' False activate when happly with code
End With
'Wait while loading
Do While appIE.Busy
DoEvents
Loop
'What aspect of the webpage to copy
Set allRowOfData = appIE.document.getElementById("nightValue0")
Dim myValue As String: myValue = allRowOfData.innerHTML
'Close the browser
appIE.Quit
Set appIE = Nothing
'Paste the data into the selected range
ActiveWorkbook.Sheets("Data_Temp").Range("C7").Value = myValue
End Sub

All you have to do is add "FirstChild" and "innertext" to your code. If I were you I would write the code differently especially on the "wait while loading" piece. It is not an efficient way to do it. Anyways, find your working code below:
Sub WebScrape_1()
'Create an Internet Explorer browser
Dim appIE As Object
Set appIE = CreateObject("internetexplorer.application")
'Browse the browser to the target webpage
With appIE
.Navigate "https://www.metoffice.gov.uk/public/weather/forecast/gcpvj0v07"
.Visible = True ' False activate when happly with code
End With
'Wait while loading
Do While appIE.Busy
DoEvents
Loop
'What aspect of the webpage to copy
Set allrowofdata = appIE.document.getElementById("nightValue0").FirstChild
allrowofdata = allrowofdata.innertext
'Close the browser
appIE.Quit
Set appIE = Nothing
'Paste the data into the selected range
ActiveWorkbook.Sheets("Sheet1").Range("C7").Value = myValue
End Sub

Related

Not able to login to a webpage using excel, error 424 object required

I am trying to login to this webpage, https://www.fois.indianrail.gov.in/ecustomer/JSP/QryInsight.jsp
using VBA. Debugging shows me that the VBA throws an error 424 object required when username line is active (apparently it is not able to fill the username data).
Here's the code:
Sub Test()
Set ie = CreateObject("InternetExplorer.application")
ie.Visible = True
ie.Navigate ("https://www.fois.indianrail.gov.in/ecustomer/JSP/QryInsight.jsp")
With ie.document
.getElementById("txtUserId").Value = "ABCDE"
.getElementById("txtPassword").Value = "ABCDE"
.getElementById("submit").Click
End With
End Sub
Can anyone help me with debugging the problem while logging in to the given webpage?
Take a look at the below example:
Option Explicit
Sub Test()
Dim oIE As Object
Set oIE = CreateObject("InternetExplorer.application")
With oIE
.Visible = True
.Navigate ("https://www.fois.indianrail.gov.in/ecustomer/JSP/QryInsight.jsp")
Do While .ReadyState < 4 Or .Busy
DoEvents
Loop
With .Document
Do While .ReadyState <> "complete"
DoEvents
Loop
With .parentWindow.frames("frmCUMain").document
.getElementsByName("txtUserId")(0).Value = "ABCDE"
.getElementsByName("txtPassword")(0).Value = "ABCDE"
.getElementsByName("cmdLogin")(0).Click
End With
End With
End With
End Sub

Press a link on a webpage using Excel VBA

I have this code where it goes to Minecraft.net and enters the username and password, and then goes to the profile page. I need to change the password on the account, which will be in cell A1 of the worksheet. I can't seem to figure out how to click the change password link. This is my code so far:
Dim IE As Object
Sub submitFeedback3()
Application.ScreenUpdating = False
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "https://minecraft.net/login"
Application.StatusBar = "Submitting"
' Wait while IE loading...
While IE.Busy
DoEvents
Wend
' **********************************************************************
IE.Document.getElementById("username").Value = "dddddddd"
IE.Document.getElementById("password").Value = "ddd"
IE.Document.getElementById("signin").Click
'**********************************************************************
Application.StatusBar = "Form Submitted"
Set IE = Nothing
Application.ScreenUpdating = True
End Sub
If the click doesn't work then you could try to submit the html form.
' Add reference to Microsoft Internet Controls
' Add reference to Microsoft HTML Object Library
Set IE = New InternetExplorer
IE.Visible = True
IE.Navigate "https://minecraft.net/login"
While IE.Busy
DoEvents
Wend
IE.Document.getElementById("username").Value = "dddddddd"
IE.Document.getElementById("password").Value = "ddd"
Dim htmlForm As HTMLFormElement
Set htmlForm = IE.Document.getElementById("loginForm")
htmlForm.submit

Scraping a single value from an HTML table and inserting into an Excel cell with VBA

Please see the code below. I am compiling a list of unusual currency pairings in excel and I wish to scrape this data with VBA. I only want to insert the value itself into the cell. Does anyone know where I am going wrong here? I am getting a 'Run-time error '91': object variable or With block variable not set'. I'm relatively new to VBA and i've put a lot a deal of thought into this.
Sub ie_open()
Dim wb As Workbook
Dim ws As Worksheet
Dim TxtRng As Range
Dim ie As Object
Set ie = CreateObject("INTERNETEXPLORER.APPLICATION")
ie.NAVIGATE "http://www.barchart.com/quotes/forex/British_Pound/Costa_Rican_Colon/%5EGBPCRC"
ie.Visible = True
While ie.ReadyState <> 4
DoEvents
Wend
Set wb = ActiveWorkbook
Set ws = wb.Sheets("Test Sheet")
Set TxtRng = ws.Range("A1")
TxtRng.Value = ie.document.getelementsbyname("divQuotePage").Item.innertext
End Sub
This is the data which I am trying to scrape:
Thanks.
I'm not that accomplished at web scraping, but that kind of error often means that what you are looking for isn't there. In particular, I don't see divQuotePage in the screen shot you provided.
But if you want the quote (793.19) you could do something like:
Dim V As Variant
Set V = ie.document.getelementbyid("dtaLast")
TxtRng = V.innertext
This will work.
Sub Test()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.Navigate "http://www.barchart.com/quotes/forex/British_Pound/Costa_Rican_Colon/%5EGBPCRC" ' should work for any URL
Do Until .ReadyState = 4: DoEvents: Loop
x = .document.body.innertext
y = InStr(1, x, "Last Price")
Z = Mid(x, y, 19)
Range("A1").Value = Trim(Z)
.Quit
End With
End Sub
You can target that element with a CSS selector of div.pricechangerow > span.last-change;
which can be simplified to .last-change.
The "." means class and you can retrieve this specific item with
Debug.Print ie.document.querySelector.querySelector(".last-change").innerText
That is for the website's current incarnation at 2018-06-30

VBA Binding modification

I have tried to modify on this sample code given to me by ron
Sub test()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
my_url = "http://www.google.com"
With IE
.Visible = True
.navigate my_url
.Top = 50
.Left = 530
.Height = 400
.Width = 400
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
End With
' Find the desired url and click
Set Results = IE.document.getElementsByTagName("a")
For Each itm In Results
If itm.outerhtml = "B.A.C. VII" Then
itm.Click
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
Exit For
End If
Next
End Sub
Now i am not a fan of late binding so i am trying to modify the first two statements from
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
and replace them with
Dim IE As InternetExplorer.Application
Set IE = New InternetExplorer.Application
However this doesn't work on my code even if i have the Microsoft Internet Controls and the the Microsoft HTML Object Library activated on references as you can see in the picture below.
WHY THAT?
Dear mehow
I have thought of that but it gives me error just look
Dim ie As InternetExplorer
Set ie = new InternetExplorer
as InternetExplorer is the class name

VBA to Enter Data Online and Submit Form

I'm trying to use VBA to submit data from Excel to a webform online. The issue happens within the * area where I'm trying to select and update the form. I've tried a variety of options (getelement by ID, name, etc.) without any success. I believe the issue has to do with identifying the appropriate element. I've seen advice about using the Locals feature in VBA, but I'm not sure how to use that for this purpose. I have a feeling someone with some experience could figure this out very quickly by looking at the source code on the webiste, using Locals in VBA, or some other technique.
The form is set up so all fields are text and I can enter/submit data online with no problem.
Thanks in advance for any help/suggestions.
Dim IE As Object
Sub submitFeedback3()
Application.ScreenUpdating = False
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "http://spreadsheetbootcamp.com/excel-efficiency-trainer-feedback/"
Application.StatusBar = "Submitting"
' Wait while IE loading...
While IE.Busy
DoEvents
Wend
**********************************************************************
IE.Document.getElementById("Form_Attempts-1372643500")(0).Value = "1"
IE.Document.getElementById("submit-1-1372643500")(0).Click
**********************************************************************
Application.StatusBar = "Form Submitted"
IE.Quit
Set IE = Nothing
Application.ScreenUpdating = True
End Sub
Try below code
Dim IE As Object
Sub submitFeedback3()
Application.ScreenUpdating = False
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "http://spreadsheetbootcamp.com/excel-efficiency-trainer-feedback/"
Application.StatusBar = "Submitting"
' Wait while IE loading...
While IE.Busy
DoEvents
Wend
' **********************************************************************
delay 5
IE.Document.getElementById("experience-1372700847").Value = "ddddd1"
delay 5
IE.Document.getElementById("Form_Time_Best-1372700847").Value = "ddddddddddd2"
delay 5
IE.Document.getElementById("submit-1-1372700847").Click
'**********************************************************************
Application.StatusBar = "Form Submitted"
IE.Quit
Set IE = Nothing
Application.ScreenUpdating = True
End Sub
Private Sub delay(seconds As Long)
Dim endTime As Date
endTime = DateAdd("s", seconds, Now())
Do While Now() < endTime
DoEvents
Loop
End Sub