VBA - Trying to extract information from webpage through HTML but can't return values inside {{ }} - excel

I am currently trying to return some data from a webpage into Excel using VBA. The website in question is linked below:
http://thecise.com/market/securities/3160
I want to extract the data from the "Category:" listing, which in this case reads "Hedge Fund".
I've been using some code put together previously from the following thread: VBA HTML Tag Hierarchy, which in its current form does pull down all the information within the HTML lists, but doesn't work for the "Category:" heading.
I'm not very experienced with HTML, but the difference appears to be that the HTML code for this heading includes some reference value contained within {{ }}, rather than the text "Hedge Fund" as with the other list items.
<li>
<span>Category:</span>
<span>{{security.s_cmpycat}}</span
</li>
Is there a way that I can adjust the code below to return the value presented on the website, rather than the raw HTML code? (i.e. return the value "Hedge Fund" rather than "{{security.s_cmpycat}}" I tried using the "From Web" data extract tool from Excel too but that didn't work either.
I realise that the code below lists all the list values rather than just the one above, but I hadn't tailored it further until I could figure out how to return the right value.
VBA Code:
Sub GetCISEDAta()
Dim xHttp As MSXML2.XMLHTTP
Dim hDoc As MSHTML.HTMLDocument
Dim hUls As MSHTML.IHTMLElementCollection
Dim hUl As MSHTML.HTMLListElement
Dim hLi As MSHTML.HTMLLIElement
Set xHttp = New MSXML2.XMLHTTP
xHttp.Open "GET", "http://thecise.com/market/securities/3160"
xHttp.send
Do
DoEvents
Loop Until xHttp.readyState = 4
Set hDoc = New HTMLDocument
hDoc.body.innerHTML = xHttp.responseText
Set hUls = hDoc.getElementsByTagName("ul")
For Each hUl In hUls
For Each hLi In hUl.Children
Debug.Print hLi.innerText
Next hLi
Next hUl
End Sub
HTML Code Section:
<div class="row">
<div class="security-listing-col security-listing-registration-data">
<ul class="dl-list list-unstyled">
<li>
<span>ISIN:</span>
<span>GG00B247XG70</span>
</li>
<li>
<span>Date Listed:</span>
<span>28-09-2007</span>
</li>
<li>
<span>Domicile:</span>
<span>Guernsey</span>
</li>
<li>
<span>Sponsor:</span>
<span>Vistra Fund Services (Guernsey) Ltd</span>
</li>
<li>
<span>Category:</span>
<span>{{security.s_cmpycat}}</span>
</li>
</ul>
</div>
<div class="security-listing-col security-listing-col-hidemd">
<div class="hr mb-20"></div>
</div>
Thanks!

Related

HTML extracted from website in Excel comes with missing data

I am trying to extract some data from a web page using the following VBA code in Excel:
Sub Extractor_data()
Dim http As New XMLHTTP60, html As New HTMLDocument
With http
.Open "GET", "https://www.pantone.com/connect/11-0105-TPX", False
.send
html.body.innerHTML = .responseText
End With
Range("A1") = html.querySelectorAll("#maincontent div div div div article div.bg")(0).innerHTML
End Sub
But what I get from it has missing data.
I get:
<div class="square" :style="squareStyle"></div>
<div class="code" v-if="color">
<p v-t="i18n.pantoneTiTle"></p>
<p v-text="color.code"></p>
<p v-if="color.name && color.name !== color.code" v-text="color.name"></p>
</div>
I was supposed to get:
<div class="square" style="background-color: rgb(239, 239, 223);"></div>
<div class="code">
<p>PANTONE</p>
<p>11-0104 TPX</p>
<p>Vanilla Ice</p>
</div>
I have tried to understand what's going on, I have tried some slightly different approaches, all to no avail.
Does anyone has an explanation and, hopefully, a fix?
Thank you.

Excel VBA: Working with iFrame via IE Automation

I have a project that I am working on where I am trying to automate a site's behavior via Excel's VBA. So far, I know how to initialize a web browser from VBA, navigate to a website, and perform a simple task such as clicking on an item using the getElementById function and click method. However, I wanted to know how can I go about working with an embedded object(s) that is inside of an iframe.
For example, here is an overview of what the tree structure looks like via HTML source code. Of course, there are a lot more tags, but at least you can get an idea of what it is that I am trying to do.
<html>
<body>
<div>
<iframe class="abs0 hw100" scrolling="no" allowtransparency="true" id="Ifrm1568521068980" src="xxxxx" title="mailboxes - Microsoft Exchange" ldhdlr="1" cf="t" frameborder="0"> <<< ----- This is where I am stuck
<div>
<tbody>
<tr>
etc.....
etc.....
etc.....
</tr>
<tbody>
<div>
----------- close tags
I guess the biggest problem for me is to learn how to manipulate an embedded object(s) that is enclosed inside of an iframe because all of this is still new to me and I am not an expert in writing programs in VBA. Any help or guidance in the right direction will help me out a lot. Also, if you need more information or clarification, please let me know.
Here the code that I have written so far:
Option Explicit
'Added: Microsoft HTML Object Library
'Added: Microsoft Internet Controls
'Added: Global Variable
Dim URL As String
Dim iE As InternetExplorer
Sub Main()
'Declaring local variables
Dim objCollection As Object
Dim objElement As Object
Dim counterClock As Long
Dim iframeDoc As MSHTML.HTMLDocument 'this variable is from stackoverflow (https://stackoverflow.com/questions/44902558/accessing-object-in-iframe-using-vba)
'Predefining URL
URL = ""
'Created InternetExplorer Object
Set iE = CreateObject("InternetExplorer.Application")
'Launching InternetExplorer and navigating to the URL.
With iE
.Visible = True
.Navigate URL
'Waiting for the site to load.
loadingSite
End With
'Navigating to the page I need help with that contains the iFrame structure.
iE.Document.getElementById("Menu_UsersGroups").Click
'Waiting for the site to load.
loadingSite
'Set iframeDoc = iE.frames("iframename").Document '<<-- this is where the error message happens: 438 - object doesn't support this property or method.
'The iFrame of the page does not have a name. Instead "Ifrm1" is the ID of the iFrame.
End Sub
'Created a function that will be used frequently.
Function loadingSite()
Application.StatusBar = URL & " is loading. Please wait..."
Do While iE.Busy = True Or iE.ReadyState <> 4: Debug.Print "loading": Loop
Application.StatusBar = URL & " Loaded."
End Function
Please note: My knowledge of programming in VBA is on an entry-level. So, please bear with me if I don't understand your answer the first time around. Plus, any nifty documentation or videos about this topic will help me a lot as well. Either way, I'm very determined to learn this language as it is becoming very fun and interesting to me especially when I can get a program to do exactly what it was designed to do. :)
You try to use the following code to get elements from the iframe:
IE.Document.getElementsbyTagName("iframe")(0).contentDocument.getElementbyId("txtcontentinput").Value = "BBB"
IE.Document.getElementsbyTagName("iframe")(0).contentDocument.getElementbyId("btncontentSayHello").Click
Sample code as below:
index page:
<input id="txtinput" type="text" /><br />
<input id="btnSayHello" type="button" value="Say Hello" onclick="document.getElementById('result').innerText = 'Hello ' + document.getElementById('txtinput').value" /><br />
<div id="result"></div><br />
<iframe width="500px" height="300px" src="vbaiframecontent.html">
</iframe>
vbaframeContent.html
<input id="txtcontentinput" type="text" /><br />
<input id="btncontentSayHello" type="button" value="Say Hello" onclick="document.getElementById('content_result').innerText = 'Hello ' + document.getElementById('txtcontentinput').value" /><br />
<div id="content_result"></div>
The VBA script as below:
Sub extractTablesData1()
'we define the essential variables
Dim IE As Object, Data As Object
Dim ticket As String
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.navigate ("<your website url>")
While IE.ReadyState <> 4
DoEvents
Wend
Set Data = IE.Document.getElementsbyTagName("input")
'Navigating to the page I need help with that contains the iFrame structure.
IE.Document.getElementbyId("txtinput").Value = "AAA"
IE.Document.getElementbyId("btnSayHello").Click
'Waiting for the site to load.
'loadingSite
IE.Document.getElementsbyTagName("iframe")(0).contentDocument.getElementbyId("txtcontentinput").Value = "BBB"
IE.Document.getElementsbyTagName("iframe")(0).contentDocument.getElementbyId("btncontentSayHello").Click
End With
Set IE = Nothing
End Sub
After running the script, the result as below:

How to select a time in a time picker while web-scraping in excel VBA

I am trying to select 09 from the time picker below using excel VBA. I have got as far as the line below but do not know how to proceed. Any assistance would be greatly appreciated.
Set e = appIE.document.getElementbyid("timepicker-pickup")
<div class="timepicker" id="timepicker-pickup">
<div class="arrow"></div>
<div class="icon close-btn"></div>
<div class="inner-time-picker clearfix">
<div class="section hours">
<div class="up-arrow"><span class="icon arrow"></span></div>
<div class="selectors">
<ul>
<li data-value="00">00</li>
<li data-value="01">01</li>
<li data-value="02">02</li>
<li data-value="03">03</li>
<li data-value="04">04</li>
<li data-value="05">05</li>
<li data-value="06">06</li>
<li data-value="07">07</li>
<li data-value="08" class="selected">08</li>
<li data-value="09">09</li>
<li data-value="10">10</li>
<li data-value="11">11</li>
</ul>
</div>
<div class="down-arrow"><span class="icon arrow"></span></div>
CSS selectors:
Try selector #timepicker-pickup li:nth-child(10)
appIE.document.querySelector("#timepicker-pickup li:nth-child(10)").Click
The # means className and so selects the 10th li tag within element with className timepicker-pickup
or selector #timepicker-pickup [data-value='09']
appIE.document.querySelector("#timepicker-pickup *[data-value='09']").Click
This is simply an abbreviation where I look for anything with attribute data-value='09' within the className of timepicker-pickup
I tested with the latest which works.
CSS selector:
VBA code:
Public Sub test()
Dim ieApp As InternetExplorer, URL As String
URL = "https://www.europcar.co.za/"
Set ieApp = New InternetExplorer
With ieApp
.Visible = True
.navigate URL
While .Busy Or .readyState < 4: DoEvents: Wend
.document.querySelector("#timepicker-pickup [data-value='09']").Click
Stop '<== Delete me
'.Quit <'== Uncomment me
End With
End Sub

VBA web scrape innertext

I have now tried for quite some time to web scrape this innertext:
I want the value 0606 copied to an Excel sheet
<TABLE class="group"
<td width="100%" nowrap="" colspan="3">
<input name="pg41_PolicyHolder_FogP_PolicyHolderId_FogP_IdentityQualifier"
type="HIDDEN" value="CPR">CPR-nr:
<input name="pg41_PolicyHolder_FogP_PolicyHolderId_FogP_IdentityValue"
type="HIDDEN" value="0606">0606</td>
I have tried through get.attribute,getelementbyclassname, value and innertext, but now I need some fresh eyes on it.
Does any of you have a good idea?
Something like this should work, however without your code I don't know how you're obtaining your HTMLDocument:
Dim oHTMLDocument As Object
Dim ele As Object
Set oHTMLDocument = ... 'No code provided so I'm unsure how you obtained the HTMLDocument
For Each ele in oHTMLDocument.getElementsByTagName("input")
If ele.Name = "pg41_PolicyHolder_FogP_PolicyHolderId_FogP_IdentityValue" Then
Debug.Print ele.innerText
Exit For
End If
Next ele
You can use a CSS selector and avoid the need for a loop.
input[name="pg41_PolicyHolder_FogP_PolicyHolderId_FogP_IdentityValue"]
Try it
VBA
.querySelector is accessed via the HTML document set when you have your page (method not shown in your question) but with Internet Explorer for example:
IE.document.querySelector("pg41_PolicyHolder_FogP_PolicyHolderId_FogP_IdentityValue").innerText
Further info:
HTML DOM querySelector() Method

getelementsbyID inner dt id values

I am extracting data from HTML using Vb Script. This is the HTML code from which am trying to extract the data.
<dl id="overview">
<dt id="overview-summary-current-title" class="summary-current" style="display:block">
Current
</dt>
<dd class="summary-current" style="display:block">
<ul class="current">
<li>
Software Engineer
<span class="at">at </span>
<a class="company-profile-public" href="/company/ABC Systems?trk=ppro_cprof">
<span class="org summary">ABC Systems</span></a>
</li>
</ul>
</dd>
In my previous question, I had asked for a similar doubt. The link is Excel getElementById extract the span class information.
However, in that case, I wanted to extract the information corresponding to the dl id and it also had span id. In this case, I need to extract the information corresponding to the dt id.
In my VB Script, I tried something like this.
Dim openedpage as String
openedpage = iedoc1.getElementById("overview").getElementById("overview-summary-current-title").innerHTML
However, I am getting no output.
I want the output as Software Engineer at ABC systems.
Kindly help me out.
The object returned by getElementById() doesn't have a method .getElementById(), so the following line fails:
.getElementById("overview").getElementById("overview-summary-current-title")
If you don't get any output, not even an error message, you probably have On Error Resume Next somewhere in your script. Please don't use that unless you know exactly what you're doing and have sensible error handling code in place.
Also, the element with the ID "overview-summary-current-title" is this:
<dt id="overview-summary-current-title" class="summary-current" style="display:block">
Current
</dt>
So you couldn't possibly extract the text "Software Engineer at ABC systems" from that element.
Try selecting the first <ul> tag from the element with the ID "overview", and then use the innerText property instead of the innerHtml property:
Set ie = CreateObject("InternetExplorer Application")
ie.Navigate "..."
While ie.Busy : WScript.Sleep 100 : Wend
Set e1 = ie.document.getElementById("overview")
Set e2 = e1.getElementsByTagName("ul")(0)
WScript.Echo e2.innerText

Resources