Solved

Analyzing HTML documents

Posted on 2002-05-03
7
295 Views
Last Modified: 2010-05-02
Hi,

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
WebBrowser1.document.Images
WebBrowser1.document.Links
WebBrowser1.document.All....
etc.
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.

Thanks
0
Comment
Question by:hveld
  • 4
  • 2
7 Comments
 
LVL 28

Accepted Solution

by:
AzraSound earned 200 total points
Comment Utility
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
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
hearing....
0
 

Author Comment

by:hveld
Comment Utility
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,
htmlDoc.images(0).src
returns
about:blankImage1.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
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
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", "")
    Next
   
    For Each htmlEle In htmlDoc.All.tags("IMG")
        htmlEle.src = Replace$(htmlEle.href, "about:blank", "")
    Next
   
    Set GetHTMLDocument = htmlDoc
End Function
0
 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
Sorry, second For...Next loop should say .src on both sides of the equation.
0
 

Author Comment

by:hveld
Comment Utility
OK, thanx!
However I didn't mean what you last suggested.
When I load a file in a webbrowser control, for
webbrowser1.Document.Images(0).src
i get something like
file:///C:\Test\Img.jpg
Using your way, I get
about:blankImg.jpg
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
0
 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
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:

C:\HTMLDocuments\MyWebpage\myPage.html

Then you know the image above must be located in:

C:\HTMLDocuments\MyWebpage\images\

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:

Frames
Javascript files
Stylesheet files
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now