How can I read the webpage source code and generate an html file?

Source: Internet
Author: User
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.

^

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.