Analyzing HTML documents


I want to analyze some html files (stored locally) and to be able to retrieve all the links, images or whatever other tags.
Now I do this by loading the file into a webbrowser control, then I can read
The problem is many files have images inserted with absolute http:// paths, try to connect to another websites(like visit counters etc), display alerts, prompts or confirmation boxes, script errors and so on.
I want to analyze the file without the user to see anything. But if he's offline, having anything that tries to access the web may have undesired results like launching phone dialers, error messages, etc. If he's online, the file will be loaded slowly because it accesses online stuffs.
Also there is no way to prevent alerts and prompts to appear. Setting offline and silent properties to True has no effect.

So my question is can I analyze a Html file without loading it into a webbrowser(or how to avoid the above problems if I use a webbrowser).
Of course I don't mean a substring search solution, like to look for "<A HREF", then to look for the closing ">" etc.

Who is Participating?
AzraSoundConnect With a Mentor Commented:
Try this:

- Set a reference to Microsoft HTML Object Library
- Load your HTML file into a string buffer using native VB file commands
- Use code similar to the following to generate an HTMLDocument object you can analyze

Public Function GetHTMLDocument(ByVal HTMLCode As String) As HTMLDocument
    Dim htmlDoc   As New HTMLDocument

    htmlDoc.body.innerhtml = HTMLCode
    Set GetHTMLDocument = htmlDoc
End Function
Richie_SimonettiIT OperationsCommented:
hveldAuthor Commented:
that's OK, thanks!
Works fine!
But this way the URL of the file to analyze is about:blank because htmlDoc is created from a string.
So I can't get absolute local paths to images or linked files, if they are included in the html document with relative paths.
Of course I can try to obtain the abs. local paths to them, having the path to the file being analyzed - but this means to change a lot of already tested code.
I tried to set
htmlDoc.location = "local_path_to_the_file"
but this opens the file in IE.
If the first image in the html file is Image1.jpg,
but I need the full local path to the image
Can this be done?
If no, I'll accept your solution as an answer and will rewrite my code
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Perhaps you can alter the function to strip the about:blank statements for you, since we know this is a limitation of loading an html document in this way.

Public Function GetHTMLDocument(ByVal HTMLCode As String) As HTMLDocument
    Dim htmlDoc     As New HTMLDocument
    Dim htmlEle     As HTMLHtmlElement

    htmlDoc.body.innerHTML = HTMLCode
    For Each htmlEle In htmlDoc.All.tags("A")
        htmlEle.href = Replace$(htmlEle.href, "about:blank", "")
    For Each htmlEle In htmlDoc.All.tags("IMG")
        htmlEle.src = Replace$(htmlEle.href, "about:blank", "")
    Set GetHTMLDocument = htmlDoc
End Function
Sorry, second For...Next loop should say .src on both sides of the equation.
hveldAuthor Commented:
OK, thanx!
However I didn't mean what you last suggested.
When I load a file in a webbrowser control, for
i get something like
Using your way, I get
If I remove about:blank, I'll still get the relative path to the image, but I need  C:\Test\Img.jpg
Replacing about:blank with the html file path will not work in all cases(like for images above the html file folder)
What I needed is to create htmlDoc from a file not from a string, in order to get the same results I get from a file loaded in a webbrowser control
Try it out, and let me know what happens.  Since image sources can be relative paths, I imagine that will still remain the same even if you load it from a file and remove the about:blank portion.  For example, an html file on my machine my say:

<img src="images/mypic.jpg">

So even if you load as a string, the about:blank will appear before that, but you should be able to reconstruct the full path to that image, if you need it, since you have the path to the actual html file itself, and now, the relative path to the image.  So in our example case, if the full path to the html file to open was:


Then you know the image above must be located in:


Be sure to do the Replace function for all elements that can have a source or href type tag.  Some that come to mind are:

Javascript files
Stylesheet files
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.