Solved

How do I extract the Title of a web page

Posted on 2014-09-12
4
156 Views
Last Modified: 2014-09-13
I am using Excel VBA to scrape a page.
Everything is work, but I can't get the title.
HTMLDoc.Title is not returning anything, but HTMLDoc.body returns the text of the body.
0
Comment
Question by:rrhandle8
  • 2
4 Comments
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 40319943
Hi,

pls try

Sub ImportEE()

Dim ie As InternetExplorer
Dim html As HTMLDocument

Set ie = New InternetExplorer
ie.Visible = False
ie.navigate "http://www.experts-exchange.com/"


Do While ie.readyState <> READYSTATE_COMPLETE
    DoEvents
Loop

Set html = ie.document

MsgBox html.Title

Set ie = Nothing

End Sub

Open in new window

Regards
0
 

Author Comment

by:rrhandle8
ID: 40319998
Here is the code I am using:

   Dim HTMLDoc As New HTMLDocument
    Dim oHttp As MSXML2.XMLHTTP
    Dim sHTML As String
    Dim AnchorLinks As Object
    Dim TDelements As Object
    Dim TDelement As Object
    Dim AnchorLink As Object

    On Error Resume Next
    Set oHttp = New MSXML2.XMLHTTP
    If Err.Number <> 0 Then
        Set oHttp = CreateObject("MSXML.XMLHTTPRequest")
        MsgBox "Error 0 has occured while creating a MSXML.XMLHTTPRequest object"
    End If
    On Error GoTo 0
    If oHttp Is Nothing Then
        MsgBox "For some reason I wasn't able to make a MSXML2.XMLHTTP object"
        Exit Sub
    End If

    'Open the URL in browser object
    oHttp.Open "GET", URL, False
    oHttp.send
    sHTML = oHttp.responseText

    'Debug.Print oHttp.responseText

    HTMLDoc.body.innerHTML = oHttp.responseText
HTMLDoc.Title
0
 

Author Comment

by:rrhandle8
ID: 40320084
Any ideas?
0
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
ID: 40320177
Following your code, you can extract the title from the responseText:

     HTMLDoc.Title = Mid(oHttp.responseText, InStr(1, oHttp.responseText, "<Title>", vbTextCompare) + 7, _
                    InStr(1, oHttp.responseText, "</Title>", vbTextCompare) - _
                    InStr(1, oHttp.responseText, "<Title>", vbTextCompare) - 7)

Open in new window


Regards,
-Glenn
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question