Extract keywords from an HTML document #68

In order to extract a meta tags from an htmldocument you need to go through the DocumentElement property of IHTMLDocument3. The following code will go through all the meta elements in a webpage.

var
  i: Integer;
  Doc: OleVariant;
begin
  Doc := MyWebBrowser2.Document;
  for i := 0 to Doc.DocumentElement.all.Length -1 do
  begin
    if Supports(
      Doc.DocumentElement.all.Item(I,0), IHTMLMetaElement
    ) then
    begin
      // this is a meta tag, cast it to IHTMLMetaElement
      // to get info from it
    end;
  end;
end;

Alternatively you can also use IHTMLDocument3.getElementsByTagName to get only the meta tags:

var
  Doc: OleVariant;
  MetaCollection: IHTMLElementCollection;
begin
  Doc := MyWebBrowser2.Document;
  MetaCollection := Doc.GetElementsbyTagName('meta');
  // Metacollection is a htmlelementcollection that contains all the
  // meta tags in the webpage
end;

Please note that MyWebBrowser2 is supposed to be a IWebBrowser2 implementation.

Found on the [Delphi-WebBrowser] news-group

Author: nybnybnybnyb
Added: 2008/11/26
Last updated: 2008/11/26