N
nau
Unregistered / Unconfirmed
GUEST, unregistred user!
给定一个url,如何直接提取该页的页面文本?
这是copy_paste给我的完整的程序代码,我编译通过了,但还是取不到页面文本,
怎么办啊?请帮帮忙!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses WinInet;
{$R *.dfm}
function DownloadFile(const AURL: string): string;
procedure Add(Buf: PChar; Count: Integer);
var
Len: Integer;
begin
Len := Length(Result);
SetLength(Result, Len + Count);
Move(Buf^,Result[Len + 1],Count);
end;
var
BytesRead: DWORD;
Session, Connection: HINTERNET;
Buffer: array[1..1024] of Char;
begin
Result := '';
Session := InternetOpen(nil, INTERNET_OPEN_TYPE_DIRECT, nil, nil, 0);
if Assigned(Session) then
try
Connection := InternetOpenUrl(Session, PChar(AURL), nil,INTERNET_FLAG_RAW_DATA,INTERNET_FLAG_RELOAD,0);
if Assigned(Connection) then
try
repeat
FillChar(Buffer, SizeOf(Buffer), 0);
InternetReadFile(Connection, @Buffer, SizeOf(Buffer), BytesRead);
if BytesRead > 0 then
Add(@Buffer, BytesRead);
Application.ProcessMessages;
until BytesRead = 0;
finally
InternetCloseHandle(Connection);
end;
finally
InternetCloseHandle(Session);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Text := DownloadFile('http://www.sina.com.cn');
end;
end.
这是copy_paste给我的完整的程序代码,我编译通过了,但还是取不到页面文本,
怎么办啊?请帮帮忙!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses WinInet;
{$R *.dfm}
function DownloadFile(const AURL: string): string;
procedure Add(Buf: PChar; Count: Integer);
var
Len: Integer;
begin
Len := Length(Result);
SetLength(Result, Len + Count);
Move(Buf^,Result[Len + 1],Count);
end;
var
BytesRead: DWORD;
Session, Connection: HINTERNET;
Buffer: array[1..1024] of Char;
begin
Result := '';
Session := InternetOpen(nil, INTERNET_OPEN_TYPE_DIRECT, nil, nil, 0);
if Assigned(Session) then
try
Connection := InternetOpenUrl(Session, PChar(AURL), nil,INTERNET_FLAG_RAW_DATA,INTERNET_FLAG_RELOAD,0);
if Assigned(Connection) then
try
repeat
FillChar(Buffer, SizeOf(Buffer), 0);
InternetReadFile(Connection, @Buffer, SizeOf(Buffer), BytesRead);
if BytesRead > 0 then
Add(@Buffer, BytesRead);
Application.ProcessMessages;
until BytesRead = 0;
finally
InternetCloseHandle(Connection);
end;
finally
InternetCloseHandle(Session);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Text := DownloadFile('http://www.sina.com.cn');
end;
end.