I was looking at the answer, by @OmegaStripes, to this question How to get a particular InnerText from a specific class? Here one uses the Split
function, and a specified delimiter string, to extract an href
from .responseBody
.
I then tried to replicate this to extract the following href
:
"https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2018/02/New-AmbSYS-to-2018-Jan.csv"
from NHS England's Ambulance Quality Indicators
HTML snippet:
<main class="main group" role="main">
<div class="page-content" id="main-content">
<header>
<h1>Ambulance Quality Indicators</h1>
</header>
<article class="rich-text">
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p><strong>CSV Data</strong><br>
These files have the same data as other published spreadsheets, but without any formatting:<br>
<a href="https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2018/02/New-AmbSYS-to-2018-Jan.csv" class="csv-link" onclick="ga('send', 'event', 'Downloads', 'CSV', 'https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2018/02/New-AmbSYS-to-2018-Jan.csv');">New Systems Indicators August 2017 to January 2018 (CSV, 23KB)</a><br>
</article>
</div>
</main>
Problem:
I am getting response text back that looks as follows:
Example response text:
From a quick bit of research, see references, I am guessing this is perhaps an encoding problem?
I tried setting a .SetRequestHeader
.setRequestHeader "Content-Type", _
"application/x-www-form-urlencoded; charset=UTF-8"
This made no difference to the output.
To be honest, I haven't a clue how to resolve this.
Any suggestions please on how I get the expected response text? i.e. that I can parse for the href
of interest.
Context:
This is part of a bigger piece of work where:
1) I want to scrape that CSV link (the name of which will change each month), without having the browser pop-up
2) Download the target file content
3) Use ADODB.Stream to write the binary file out.
This process was outlined by @OmegaStripes in response to my question Return focus to ThisWorkbook.Activesheet after XMLHTTP60 file download . I am trying to understand and implement that suggestion currently.
Code:
Option Explicit
Public Const url As String = "https://www.england.nhs.uk/statistics/statistical-work-areas/ambulance-quality-indicators/"
Public aBody As String
Sub Testing()
' Download via XHR
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", url, False
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded; charset=utf-8"
.send
' Get binary response content
aBody = .responseBody
End With
ActiveSheet.Range("A1") = aBody
End Sub
References:
1) XMLHTTP and Special Characters (eg, accents)
2) setRequestHeader Method (IXMLHTTPRequest)