Reputation: 2859
I am wondering why the response property of an MSXML2.ServerXMLHTTP object not returning the full html source. It appears it is only returning the "inner html". I can create an IE object and get the "outer html" but that is not too efficient since I have hundreds of search items.
I have the function shown below (with the URL) that assigns the HTML content to a string.
Sub test()
Dim myString As String
myString = getECICS2("103-90-2") ' myString only contains inner html
End Sub
Public Function getECICS(ByVal casNum As String) As String
Dim XMLhttp: Set XMLhttp = CreateObject("MSXML2.ServerXMLHTTP")
XMLhttp.setTimeouts 2000, 2000, 2000, 2000
XMLhttp.Open "GET", "http://ec.europa.eu/taxation_customs/dds2/ecics/chemicalsubstance_consultation.jsp?Lang=en&Cas=" & casNum & "&Cus=&CnCode=&EcCode=&UnCode=&Name=&LangNm=en&Inchi=&Characteristic=&sortOrder=1&Expand=true&offset=0&range=25", False
XMLhttp.send
If XMLhttp.Status = 200 Then
getECICS = XMLhttp.responseText
Else
getECICS = ""
End If
End Function
Thanks in advance
Upvotes: 0
Views: 4913
Reputation:
Tim has hit the nail on the head. The webpage uses javascript to update the page once the html has been downloaded. This happens automatically in a browser.
If you run the code below it will dump the response into an html file which you can view in Chrome/IE/FF etc
Sub test()
Dim myString As String
myString = getECICS("103-90-2") ' myString only contains inner html
End Sub
Public Function getECICS(ByVal casNum As String) As String
Dim XMLhttp: Set XMLhttp = CreateObject("MSXML2.ServerXMLHTTP")
XMLhttp.setTimeouts 2000, 2000, 2000, 2000
XMLhttp.Open "GET", "http://ec.europa.eu/taxation_customs/dds2/ecics/chemicalsubstance_consultation.jsp?Lang=en&Cas=" & casNum & "&Cus=&CnCode=&EcCode=&UnCode=&Name=&LangNm=en&Inchi=&Characteristic=&sortOrder=1&Expand=true&offset=0&range=25", False
XMLhttp.send
If XMLhttp.Status = 200 Then
getECICS = XMLhttp.responseText
Else
getECICS = ""
End If
outputtext (getECICS)
End Function
Function outputtext(text As String)
Dim MyFile As String, fnum As String
MyFile = ThisWorkbook.Path & "\" & "test.html"
'set and open file for output
fnum = FreeFile()
Open MyFile For Output As fnum
'use Print when you want the string without quotation marks
Print #fnum, text
Close #fnum
End Function
Unfortunately, the easiest solution is to run your automation in a browser or script enabled solution to get at the required data.
Many sites now use javascript/AJAX/Login sessions to control the speed and access to resources these days so you cannot always get the desired speed insreases by not using a browser.
Upvotes: 1
Reputation: 1477
Have look at the other methods of XMLHttpRequest
...
responseText
returns the response body as text
responseXML
returns the body as a DOM object
I think you are after: XMLhttp.response
which returns the whole response.
or maybe: XMLhttp.responseBody
?
I'm not totally sure on this 'cos I've only used the C++ interface myself.
see: http://msdn.microsoft.com/en-us/library/windows/apps/hh453379.aspx#methods
Upvotes: 0