我怎么用delphi获取某网页文件内容呢?(50分)

  • 主题发起人 主题发起人 jxzqsun
  • 开始时间 开始时间
J

jxzqsun

Unregistered / Unconfirmed
GUEST, unregistred user!
比如说我想知道http://www.163.com/的首页地址的文件内容并保存到demo1中该怎么做?
 
搜一下 Webbrowser
 
唉,很容易的,,用一个TNMHTTP控件就行了
用GET方法取网页,返回的内容,在BODY属性里
 
写winsocks,connect 80端口,然后GET /. HTTP/1.0,或许用clientsocket connect 80
sendtext('GET /. HTTP/1.0');就好了,很多办法
 
用一个TNMHTTP控件就搞定~~
 
用一个nmhttp1控件:这是我的一个程序给你了
nmhttp1.Get('http://women.sohu.com/xingzuo/c.php?type='+typ+'&yyear='+year+'+&mmonth='+month+'&dday='+day+'');
content:=nmhttp1.Body;
k:=pos('今日运势',content);
content:=copy(content,k,length(content));
tmp:='<font color="#003300">';
startpos:=pos(tmp,content)+length(tmp);
content:=copy(content,startpos,length(content));
endpos:=pos('</font>',content)-1;
content:=copy(content,1,endpos);
memo1.Lines.Add(content);
 
delphi下面介绍TNMHTTP控件就有例子
 
建议你用ICS的HTTP控件,比较稳定,delphi自带的太差了,不好用!如要索取ICS控件的读
取涛程序,请给我写信
 
unit Unit2;

interface

uses
Contnrs, Classes, SysUtils, ComCtrls, SHDocVw, MSHtml, OleServer, ActiveX;

type
TBrower=class
FOnComplete:TNotifyEvent;
private
procedure SetOnComplete(const Value: TNotifyEvent);
procedure doOnComplete;
public
property OnComplete:TNotifyEvent read FOnComplete write SetOnComplete;
function GetHTML:String;virtual;abstract;
procedure Navigate(UrlStr:String);virtual;abstract;
class function getInstance:TBrower;
class procedure FreeAndNilInstance;
end;

TIEBrowerAdapter=class(TBrower)
private
IE:TInternetExplorer;
procedure Downloaded(Sender: TObject; var pDisp: OleVariant;var URL: OleVariant);
public
function GetHTML:String;override;
procedure Navigate(UrlStr:String);override;
constructor Create;
destructor Destroy;override;
end;

implementation

{ TBrower }

var
Brower:TBrower;

procedure TBrower.doOnComplete;
begin
if Assigned(FOnComplete) then
FOnComplete(Self);
end;

class procedure TBrower.FreeAndNilInstance;
begin
FreeAndNil(Brower);
end;

class function TBrower.getInstance: TBrower;
begin
if not Assigned(Brower) then
Brower:=TIEBrowerAdapter.Create;
Result:=Brower;
end;

procedure TBrower.SetOnComplete(const Value: TNotifyEvent);
begin
FOnComplete := Value;
end;

{ TIEBrowerAdapter }

constructor TIEBrowerAdapter.Create;
begin
IE:=TInternetExplorer.Create(nil);
IE.ConnectKind:=ckNewInstance;
IE.OnDocumentComplete:=Downloaded;
IE.Visible:=True;
IE.Connect;
end;

destructor TIEBrowerAdapter.Destroy;
begin
IE.Quit;
IE.Free;
inherited;
end;

procedure TIEBrowerAdapter.Downloaded(Sender: TObject; var pDisp,
URL: OleVariant);
begin
doOnComplete;
end;

function TIEBrowerAdapter.GetHTML: String;
var
IpStream: IPersistStreamInit;
Stm: TMemoryStream;
st: TStrings;
begin
Result:='';
if Assigned(IE.Document) then
begin
Stm := TMemoryStream.Create;
st := TStringList.Create;
IpStream := IDispatch(IE.Document) as IPersistStreamInit;
IpStream.save(TStreamAdapter.Create(Stm), TRUE);
Stm.Position := 0;
st.LoadFromStream(Stm);
Result:=st.Text;
Stm.Free;
st.Free;
end;
end;

procedure TIEBrowerAdapter.Navigate(UrlStr: String);
begin
IE.Navigate(UrlStr);
end;

end.
 
后退
顶部