How to access HTML file Document created through XMLHTTP request - vba

I have got a responseText from an XMLHTTP request:
Set XMLHttp = CreateObject("MSXML2.XMLHTTP")
XMLHttp.Open "GET", urlPiece, False
XMLHttp.send
that I store in an HTML file created in memory:
Set htmlResponse = CreateObject("htmlfile")
htmlResponse.body.innerHTML = XMLHttp.responseText
If I look at the object htmlResponse on the debugger, I see the structure of a normal HTML file. However, when I try to get the document, I don't succeed:
Set doc = htmlResponse.document '<-- Invalid method or property
What am I doing wrong? Below my full code in case you want to test on real sample:
Sub getPrice()
Dim urlPiece As String: urlPiece = "https://fr.finance.yahoo.com/q?s="
Dim htmlResponse As Object
Dim XMLHttp As Object
Set htmlResponse = CreateObject("htmlfile")
ccyPair = "XAUUSD"
urlPiece = urlPiece & ccyPair & "=X"
Set XMLHttp = CreateObject("MSXML2.XMLHTTP")
XMLHttp.Open "GET", urlPiece, False
XMLHttp.send
htmlResponse.body.innerHTML = XMLHttp.responseText
Set doc = htmlResponse.document '<-- error here
End Sub

I have found the mistake myself.
Differently than JavaScript, the document is defined in the body of the HTMLfile and is not itself an attribute of the object.
Hence:
Set doc = htmlResponse.document
should rather be
Set doc = htmlResponse.body.document

Related

Extract Data from URL VBA

I am trying to get Addresses Data from URL but facing some error. I am just beginner in VBA, i did not Understand where is problem in my code. wish somebody can help me to get right solution.
here I attached Image and also my VBA code
here is my Code
Public Sub IE_GetLink()
Dim sResponse As String, HTML As HTMLDocument
Dim url As String
Dim Re As Object
Set HTML = New HTMLDocument
Set Re = CreateObject("MSXML2.XMLHTTP")
'On Error Resume Next
url = "http://markexpress.co.in/network1.aspx?Center=360370&Tmp=1656224682265"
With Re
.Open "GET", url, False
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
.send
sResponse = StrConv(.responseBody, vbUnicode)
End With
Dim Title As Object
With HTML
.body.innerHTML = sResponse
Title = .querySelectorAll("#colspan")(0).innerText
End With
MsgBox Title
End Sub
Please help me ...
Several things.
What is wrong with your code:
Title should be a string as you are attempting to assign the return of .innerText to it. You have declared it as an object which would require SET keyword (and the removal of the .innerText accessor).
Colspan is an attribute not an id so your css selector list is incorrect.
Furthermore, looking at what the page actually does, there is a request for an additional document which actually has the info you need. You need to take the centre ID you already have and change the URI you make a request to.
Then, you want only the first td in the target table. Change your CSS selector list to target that.
Public Sub GetInfo()
Dim HTML As MSHTML.HTMLDocument
Dim re As Object
Set HTML = New MSHTML.HTMLDocument
Set re = CreateObject("MSXML2.XMLHTTP")
Dim url As String
Dim response As String
url = "http://crm.markerp.in/NetworkDetail.aspx?Center=360370&Tmp="
With re
.Open "GET", url, False
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
.send
response = .responseText
End With
Dim info As String
With HTML
.body.innerHTML = response
info = .querySelector("#tblDisp td").innerText
End With
MsgBox info
End Sub

getElementsByClassName() failed in my VBA

contents in Webpage as follow:
<pre>
<div class="catdivlogo">
<img src="//static.designandreuse.com/sip/logo/rambus2.gif" border="0" class="catlogo" alt="Rambus, Inc.">
</div>
</pre>
I failed to get the middle line by .getElementsByClASSnAME("catlogo"). I can only get the first line by .getElementsByClASSnAME("catlogo").
Anybody can help this?
My VBA as follow:
Sub Test()
Const URL As String = "https://www.design-reuse.com/sip/pci-express-c-430/"
Const CLASS_NAME As String = "catlogo"
Dim oDoc As MSHTML.HTMLDocument
Dim results As Object
With CreateObject("MSXML2.XMLHttp")
.Open "GET", URL, False
.send
'Check the response is valid
If .Status = 200 Then
Set oDoc = CreateObject("htmlfile")
oDoc.body.innerHTML = .responseText
Set results = oDoc.getElementsByClassName(CLASS_NAME)
End If
End With
End Sub

Unable to collect links of different properties from a webpage

I've written a script in vba to get only the links of different properties under the title Single Family Homes from the right sided area of a webpage. When I run my script, I get nothing, no error either. The content I wish to grab are static and available within page source, so XMLHttpRequestshould do the trick.
Although it seems the selectors I've defined within my script is errorless, I can't still fetch the links of different properties.
Webpage address
I've written:
Sub GetLinks()
Const link$ = "https://www.zillow.com/homes/for_sale/33125/house_type/12_zm/0_mmm/"
Dim oHttp As New XMLHTTP60, Html As New HTMLDocument
Dim I&
With oHttp
.Open "GET", link, False
.setRequestHeader "User-Agent", "Mozilla/5.0"
.send
Html.body.innerHTML = .responseText
With Html.querySelectorAll("article > a.list-card-info")
For I = 0 To .Length - 1
Sheet1.Range("A1").Offset(I, 0) = .item(I).getAttribute("href")
Next I
End With
End With
End Sub
Expected links are like:
https://www.zillow.com/homedetails/3446-NW-15th-St-Miami-FL-33125/43822210_zpid/
https://www.zillow.com/homedetails/1877-NW-22nd-Ave-Miami-FL-33125/43823838_zpid/
https://www.zillow.com/homedetails/1605-NW-8th-Ter-Miami-FL-33125/43825765_zpid/
How can I get all the links of different properties from it's landing page from the link above?
Use the class of the child alone. Note there are a number of other things I would like to change about the code but know you like to keep your structure/style.
Sub GetLinks()
Const link$ = "https://www.zillow.com/homes/for_sale/33125/house_type/12_zm/0_mmm/"
Dim oHttp As New XMLHTTP60, Html As New HTMLDocument
Dim I&
With oHttp
.Open "GET", link, False
.setRequestHeader "User-Agent", "Mozilla/5.0"
.send
Html.body.innerHTML = .responseText
With Html.querySelectorAll(".list-card-info")
For I = 0 To .Length - 1
Sheet1.Range("A1").Offset(I, 0) = .item(I).getAttribute("href")
Next I
End With
End With
End Sub
Some of the changes I might make:
Private Sub GetLinks()
Const LINK As String = "https://www.zillow.com/homes/for_sale/33125/house_type/12_zm/0_mmm/"
Dim http As MSXML2.XMLHTTP60, html As MSHTML.HTMLDocument
Dim i As Long, links As Object
Set http = New MSXML2.XMLHTTP60: Set html = New MSHTML.HTMLDocument
With http
.Open "GET", LINK, False
.setRequestHeader "User-Agent", "Mozilla/5.0"
.send
html.body.innerHTML = .responseText
End With
Set links = html.querySelectorAll(".list-card-info")
With ThisWorkbook.Worksheets("Sheet1")
For i = 0 To links.Length - 1
.Cells(i + 1, 1) = links.item(i).href
Next i
End With
End Sub

VERY strange behavior on createobject("HTMLFILE")!

I don't understand why I'm getting this strange behavior!
When creating and assigning the htmlfile object the function gives back a blank object ("nothing") and when I am running the code line by line it just runs automatically even when I don't press F8 to run the next line...
It gives no error whatsoever!
Any ideas as to what might be happening?
Line where the strange behavior starts: Set htmlObj = CreateObject("HTMLFILE")
Public Function XMLHTTP_Request(Method As String, URL As String, Optional PostData As String, Optional StrCookie As String) As HTMLDocument
Dim oXMLHTTP As Object, htmlObj as object
Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP")
oXMLHTTP.Open Method, URL, False
oXMLHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
oXMLHTTP.setRequestHeader "Cookies", StrCookie
On Error GoTo ErrorHandler
oXMLHTTP.send (PostData)
On Error GoTo 0
While oXMLHTTP.ReadyState <> 4: DoEvents: Wend
While oXMLHTTP.Status <> 200: DoEvents: Wend
Set htmlObj = CreateObject("HTMLFILE")
htmlObj.body.innerHTML = oXMLHTTP.responseText
Set XMLHTTP_Request = htmlObj
End Function
Repeated calls to the function will cause multiple calls to the CreateObject function. The oXMLHTTP and htmlObj object vars could be made static or library references could be included and the variable declaration changed to Early Binding.
Early binding requires that the following non-default library references are added through the VBE's Tools ► References command.
Microsoft HTML Object Library
Microsoft Internet Controls
Microsoft XML 6.0 (your own version may vary slightly).
Module1 code sheet:
Option Explicit
Sub main()
Debug.Print Left(XMLHTTP_Request("http//example.com").body.innerText, 512)
End Sub
Public Function XMLHTTP_Request(URL As String, _
Optional Method As String = "POST", _
Optional PostData As String = "", _
Optional StrCookie As String = "") As HTMLDocument
Dim oXMLHTTP As New MSXML2.XMLHTTP60
Dim htmlObj As New HTMLDocument
oXMLHTTP.Open Method, URL, False
oXMLHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
oXMLHTTP.setRequestHeader "Cookies", StrCookie
oXMLHTTP.send PostData
If oXMLHTTP.Status <> 200 Then Exit Function
htmlObj.body.innerHTML = oXMLHTTP.responseText
Set XMLHTTP_Request = htmlObj
End Function
Running the main() sub procedure will output the first 512 characters of the web page's text to the Immediate window ([Ctrl]+G).

Macro / VBA to fetch values from www.Eppraisal.com

I need to fetch some values from www.Eppraisa.com using Excel Macro.
But I don't know what should be the value of PropID. That's why the macro works for URL1 but not for URL2 because I think URL2 has a wrong propID
Const URL1 As String = "http://www.eppraisal.com/home-values/property_lookup_eppraisal?a=1122%20E%20Loyola%20Dr&z=85282&propid=42382460"
Const URL2 As String = "http://www.eppraisal.com/home-values/property_lookup_eppraisal?a=19732%20E%20Reins%20Rd&z=85142&propid=31402642"
Sub xmlHttp()
Dim xmlHttp As Object
Set xmlHttp = CreateObject("MSXML2.XMLHTTP")
' This works
xmlHttp.Open "GET", URL1, False
' But doesn't work for below url :(
'xmlHttp.Open "GET", URL2, False
xmlHttp.setRequestHeader "Content-Type", "text/xml"
xmlHttp.send
Dim ieDom As New HTMLDocument
Dim html As Object
Set html = CreateObject("htmlfile")
html.body.innerHTML = xmlHttp.responseText
Debug.Print html.body.innerHTML
ieDom.body.innerHTML = xmlHttp.responseText
For Each ieInp In ieDom.getElementsByTagName("p")
If ieInp.className = "ColorAccent6 FontBold FontSizeM Margin0 Padding0" Then
strEppraisalValue = ieInp.innerText
ElseIf ieInp.className = "FontSizeA Margin0 DisplayNone HighLow" Then
strEppraisalHighLow = ieInp.innerText
End If
Next End Sub
With Mozilla Firefox & Firebug you can identify the request and response.
Below step applies to any search you make.
1 Copy the below URL to Firefox Browser.
http://www.eppraisal.com/home-values/property/1122-e-loyola-dr-tempe-az-85282-42382460/
2 Open Up FireBug and look for below request. Goto Net Tab > XHR as in below image.
3 Expand the node and goto Params tab. It shows all the input parameters which needs to go with the GET request.
.
4 Finally we can see the response from server in the Response Tab.