How can I read the webpage source code and generate an html file? Delphi/Windows SDK/API
Http://www.delphi2007.net/DelphiBase/html/delphi_20061219212425113.html
I haven't found it online for a long time. I don't know which function can be used for implementation? Hope to give the code segment. Thank you. Please wait online.
Uses ActiveX, ComObj;
//...
Function GetHtml (const WebBrowser: TWebBrowser): string; // Lu Xiaohai, original author of this function
Const
BufSize =$ 10000;
Var
Size: Int64;
Stream: IStream;
HHTMLText: HGLOBAL;
Psi: IPersistStreamInit;
Begin
If not Assigned (WebBrowser. Document) then Exit;
OleCheck (WebBrowser. Document. QueryInterface (IPersistStreamInit, psi ));
Try
HHTMLText: = GlobalAlloc (GPTR, BufSize );
If 0 = hHTMLText then RaiseLastWin32Error;
OleCheck (CreateStreamOnHGlobal (hHTMLText, True, Stream ));
Try
OleCheck (psi. Save (Stream, False ));
Size: = StrLen (PChar (hHTMLText ));
SetLength (Result, Size );
CopyMemory (PChar (Result), Pointer (hHTMLText), Size );
Finally
Stream: = nil;
End;
Finally
Psi: = nil;
End;
End;
Procedure TForm1.Button1Click (Sender: TObject );
Var
HtmF: Textfile;
Begin
Memo1.Text: = gethtml (WebBrowser1 );
Assignfile (HtmF, 'c: \ result.htm ');
Rewrite (HtmF );
Writeln (HtmF, memo1.text );
Closefile (HtmF );
End;
Procedure TForm1.Button2Click (Sender: TObject );
Begin
Webbrowser1.Navigate ('HTTP: // www.google.com ');
End;
------------ From monopoly.
Use the webbrower control.
Webbrower. nagivate ('your url ')
8. Get the webpage source code and Html directly from TWebBrowser
The following describes an extremely simple method to obtain the web page source code that TWebBrowser is accessing. The general method is to use the IPersistStreamInit interface provided by the Document object in the TWebBrowser control, specifically: first check WebBrowser. if the Document object is valid or not, exit. Then, obtain the IPersistStreamInit interface, obtain the HTML source code size, allocate the global heap memory block, create a stream, and write the HTML text to the stream. Although the program is not complex, there are simpler methods, so the implementation code is not given. In fact, basically all the features of IE TWebBrowser should be implemented in a relatively simple way, the same is true for obtaining the web page source code. The following code displays the webpage source code in memo1.
Memo1.Lines. Add (IHtmlDocument2 (WebBrowser1.Document). Body. OuterHtml );
At the same time, it is easy to save the HTML file as a text file when you use TWebBrowser to browse the HTML file. No Syntax Parsing tools are required because TWebBrowser is also complete, as shown below:
Memo1.Lines. Add (IHtmlDocument2 (WebBrowser1.Document). Body. OuterText );
Lihuasoft)
Very deep
You cannot understand
Simple is the best. I copied others. Throwing others.
^