idhttp url汉字中文
IdHTTP1->URL->URLEncode()就可以了
http://delphi.about.com/od/internetintranet/a/extract-links-from-a-html-page-using-delphi.htm
http://delphi.about.com/od/vclusing/a/wbsinkevents.htm
做个标记
In most situations you use the TWebBrowser to display HTML documents to the user - thus creating your own version of the (Internet Explorer) Web browser.
A very nice feature of a Browser is to display link information, for example, in the status bar, when the mouse hovers over a link in a document. This can also be done in Delphi: Get the Url of a Hyperlink when the Mouse moves Over a TWebBrowser Document.
Sometimes, you "only" want to extract all the links from a HTML document / URL. You want to get the HREF attribute of all A tags.
Here's how to extract all hyperlinks from an HTML document. The ExtractLinks procedure fills a TStrings object with the value of the HREF attribute of the A HTML element.
Extract HyperLinks
uses mshtml, ActiveX, COMObj, IdHTTP, idURI;
//extract "href" attribute from A tags from an URL - into a TStrings
procedure ExtractLinks(const url: String; const strings: TStrings) ;
var
iDoc : IHTMLDocument2;
strHTML : string;
v : Variant;
x : integer;
links : OleVariant;
docURL : string;
URI : TidURI;
aHref : string;
idHTTP : TidHTTP;
begin
strings.Clear;
URI := TidURI.Create(url) ;
try
docURL := 'http://' + URI.Host;
if URI.Path <> '/' then docURL := docURL + URI.Path;
finally
URI.Free;
end;
iDoc := CreateComObject(Class_HTMLDOcument) as IHTMLDocument2;
try
iDoc.designMode := 'on';
while iDoc.readyState <> 'complete' do Application.ProcessMessages;
v := VarArrayCreate([0,0],VarVariant) ;
idHTTP := TidHTTP.Create(nil) ;
try
strHTML := idHTTP.Get(url) ;
finally
idHTTP.Free;
end;
v[0]:= strHTML;
iDoc.write(PSafeArray(System.TVarData(v).VArray)) ;
iDoc.designMode := 'off';
while iDoc.readyState<>'complete' do Application.ProcessMessages;
links := iDoc.all.tags('A') ;
if links.Length > 0 then
begin
for x := 0 to -1 + links.Length do
begin
aHref := links.Item(x).href;
if (aHref[1] = '/') then
aHref := docURL + aHref
else if Pos('about:', aHref) = 1
then aHref := docURL + Copy(aHref, 7, Length(aHref)) ;
strings.Add(aHref) ;
end;
end;
finally
iDoc := nil;
end;
end;
Get the Url of a Hyperlink when the Mouse moves Over a TWebBrowser Document
![]()
The TWebBrowser Delphi component provides access to the Web browser functionality from your Delphi applications.
In most situations you use the TWebBrowser to display HTML documents to the user - thus creating your own version of the (Internet Explorer) Web browser. Note that the TWebBrowser can also display Word documents, for example.
A very nice feature of a Browser is to display link information, for example, in the status bar, when the mouse hovers over a link in a document.
The TWebBrowser does not expose an event like "OnMouseMove". Even if such an event would exist it would be fired for the TWebBrowser component - NOT for the document being displayed inside the TWebBrowser.
Globalize your Delphi applications easy, professionally and fast!Private VPN for Facebook & Youtube Unblock Websites : 7-Day Free TrialGive your app fast OLAP functions! Delphi and C++Builder componentsIn order to provide such information (and much more, as you will see in a moment) in your Delphi application using the TWebBrowser component, a technique called "events sinking" must be implemeted.
WebBrowser Event Sink
To navigate to a web page using the TWebBrowser component you call the Navigate method. The Document property of the TWebBrowser returns an IHTMLDocument2 value (for web documents). This interface is used to retrieve information about a document, to examine and modify the HTML elements and text within the document, and to process related events.To get the "href" attribute (link) of an "a" tag inside a document, while the mouse hovers over a document, you need to react on the "onmousemove" event of the IHTMLDocument2.
Here are the steps to sink events for the currently loaded document:
- Sink the WebBrowser control's events in the DocumentComplete event raised by the TWebBrowser. This event is fired when the document is fully loaded into the Web Browser.
- Inside DocumentComplete, retrieve the WebBrowser's document object and sink the HtmlDocumentEvents interface.
- Handle the event you are interested in.
- Clear the sink in the in BeforeNavigate2 - that is when the new document is loaded in the Web Browser.
Professional FireMonkey Components Grids, Editors, Searching, ValidationHTML Document OnMouseMove
Since we are interested in the HREF attribute of an A element - in order to show the URL of a link the mouse is over, we will sink the "onmousemove" event.The procedure to get the tag (and its attributes) "below" the mouse can be defined as:
var htmlDoc : IHTMLDocument2; ... procedure TForm1.Document_OnMouseOver; var element : IHTMLElement; begin if htmlDoc = nil then Exit; element := htmlDoc.parentWindow.event.srcElement; elementInfo.Clear; if LowerCase(element.tagName) = 'a' then begin ShowMessage('Link, HREF : ' + element.getAttribute('href',0)]) ; end else if LowerCase(element.tagName) = 'img' then begin ShowMessage('IMAGE, SRC : ' + element.getAttribute('src',0)]) ; end else begin elementInfo.Lines.Add(Format('TAG : %s',[element.tagName])) ; end; end; (*Document_OnMouseOver*)As explained above, we attach to the onmousemove event of a document in the OnDocumentComplete event of a TWebBrowser:
procedure TForm1.WebBrowser1DocumentComplete( ASender: TObject; const pDisp: IDispatch; var URL: OleVariant) ; begin if Assigned(WebBrowser1.Document) then begin htmlDoc := WebBrowser1.Document as IHTMLDocument2; htmlDoc.onmouseover := (TEventObject.Create(Document_OnMouseOver) as IDispatch) ; end; end; (*WebBrowser1DocumentComplete*)And this is where the problems arise! As you might guess the "onmousemove" event is *not* a usual event - as are those we are used to work with in Delphi.
The "onmousemove" expects a pointer to a variable of type VARIANT of type VT_DISPATCH that receives the IDispatch interface of an object with a default method that is invoked when the event occurs.
In order to attach a Delphi procedure to "onmousemove" you need to create a wrapper that implements IDispatch and raises your event in its Invoke method.
Here's the TEventObject interface:
Here's the full source code along with a sample project you can download .TEventObject = class(TInterfacedObject, IDispatch) private FOnEvent: TObjectProcedure; protected function GetTypeInfoCount(out Count: Integer): HResult; stdcall; function GetTypeInfo(Index, LocaleID: Integer; out TypeInfo): HResult; stdcall; function GetIDsOfNames(const IID: TGUID; Names: Pointer; NameCount, LocaleID: Integer; DispIDs: Pointer): HResult; stdcall; function Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer; Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult; stdcall; public constructor Create(const OnEvent: TObjectProcedure) ; property OnEvent: TObjectProcedure read FOnEvent write FOnEvent; end;

浙公网安备 33010602011771号