1

I am using VBA to scrape data from an html document. The document has an iframe within it. I can use the web browser's "inspect element" an see the text; however, I cannot get to it with VBA. Here is the code, I am using, it has worked well with other elements.

Sub NewMain()

Dim i As Integer
Dim IE As InternetExplorer

Dim sc As MSHTML.IHTMLElementCollection
Dim scObj As MSHTML.IHTMLGenericElement

Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
Debug.Print "ie count " & IE_count
For x = 0 To (IE_count - 1)
   On Error Resume Next    
     my_URL = objShell.Windows(x).Document.Location
     my_title = objShell.Windows(x).Document.title


 If my_URL like "http://www.loopnet.com/xNet/Mainsite/Report/Report.aspx?"      & "*" Then

       Debug.Print "You have the document"
       Set IE = objShell.Windows(x)
       Set mydoc = IE.Document

       Set sc = mydoc.getElementsByTagName("script")
           For Each scObj In sc
                Debug.Print scObj.innerText
           Next


   End If
Next


End Sub

The html that I am trying to read is below. I am trying to get the data in a script tag that is inside the body element; although, I am also thinking the I can just access the html class="dj_ie je_ie10 dj_contentbox" id="ln" item. However, I am at a loss on how to do it. Any help would be greatly appreciated.

    <iframe name="reportFrame" width="1100" height="1100" class="report-  
     frame" id="reportFrame" src="http://reporting.loopnet.com"
     frameborder="0" scrolling="no" style="height: 2530px;"></iframe>

     <!DOCTYPE html>
     <html class="dj_ie dj_ie10 dj_contentbox" id="ln">
       <head>...</head>
       <body>...</body>
     </html>
    </iframe>

the primary website is open source. LoopNet.com they list real estate for sale and lease. looking at the site, you select properties interest, then, you click "create a report". This html snippet and what I am trying to scrape the data from is the "listing comparison report".

QHarr
  • 83,427
  • 12
  • 54
  • 101
RayK
  • 11
  • 4
  • If you cannot provide the credentials to reach the web page you are trying to scrape then at least provide a web page URL that duplicates the situation but is openly available to the general public without credentials. –  Sep 10 '15 at 00:30
  • Jeeped, thanks for the direction. This was the first time I have used Stack and I wasn't quite sure what to include. – RayK Sep 11 '15 at 08:39

1 Answers1

1

Can't see your script tag in the above.

You could start with

Dim a As Object
Set a = ie.document.frames("reportFrame").document.querySelector("#ln")

or

Set a = ie.document.frames("reportFrame").document.querySelector("#ln script")

This is based on grabbing first match. Look into .querySelector all to return a nodeList if expecting more than one and requiring an element other than the first.

See the following answer to a SO question for more discussion: Accessing object in iframe using VBA

QHarr
  • 83,427
  • 12
  • 54
  • 101