难!高手进来看看这个Button1Click怎么写!200分 (200分)

那只有自己写简单的网页解释器啦。
 
你想想就知道,你在屏幕上看到的是经过IE的内核解释执行过的结果
如果你不想用那控件,可以自己写一个啊,呵呵
其实那东西招待速度还可以嘛,我曾经研究过,它可以做很多自动化的功能。
如果要自动向URL发送东西的话,就嫌太慢了,呵呵
你又想干什么?
 
Rocklee 继续看,
如果用Memo3.Lines.Add(IHtmlDocument2(WebBrowser1.Document).Body.OuterText);
的话经常会出现取不到网页文本的情况,不过有时又可以,我是XP系统,不知为什么
----------
user mshtml
----
procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
const pDisp: IDispatch
var URL: OleVariant);
begin
Memo1.Lines.Add(IHtmlDocument2(WebBrowser1.Document).Body.OuterText);//取网页文本,非HTML代码
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
WebBrowser1.Navigate(edit3.text)
//打开EDIT3里的地址
end;
 
是的WWWVW,我也有这样的情况发生
 
是啊 我试了 好像 有问题
 
把所有tag去掉不就是text了,普通的替换啊
 
DelphiFish 具体说说你的想法
 
我可没试过耶,如果真的要取跟看到的是一个样子的文字,你不如向这个对象发个复制的消息
把那些文字拷到粘贴板中,然后倒出来,就完全一样了,哈哈。
我没时间试这个了。
这里我贴出一段我在“风殆”工具中分析webform的代码:

type
TInputItem = record
Type_: string[10];
Dict: string[255];
Value: string[255];
splitThread: Boolean;
Start: integer;
End_: integer;
_Stream: TStringlist;
end;
pInputItem = ^TInputItem;
TWebForm = record
toString: string[25];
method: string[6];
name: string[25];
target: string[25];
action: string[255];
Elements: Tstrings;
Cookies: string[255];
Lock: Boolean;
end;
PWebForm = ^TWebForm;

...
function GetHtml(const WebBrowser: TWebBrowser): string;
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;

function GetForms(Doc: IHTMLDocument2
var Forms: Tlist): Boolean;
var
ElementCollection1, ElementCollection: IHTMLElementCollection;
HtmlElement: IHTMLelement;
FormItem: IHTMLelement;
InputItem: IHTMLInputElement;
SelectItem:IHTMLSelectElement;
idx, J, I: Integer;
f_tagName: string;
form: HTMLFormElement;
ele: Tstrings;
webf: Pwebform;
Input_: pInputItem;
prot, host, path, d, port, b: string;
begin
if Doc = nil then begin
result := false;
exit;
end;
ParseURI(doc.url, prot, host, path, d, port, b);
ElementCollection := Doc.all;
ClearForms(forms, '', true);

for I := 0 to ElementCollection.length - 1 do
begin
HtmlElement := ElementCollection.item(I, '') as IHTMLElement;
if uppercase(HTMLElement.tagName) = 'FORM' then begin
form := HTMLElement as HTMLFormElement;
idx := ExistsForm(form.name, Forms);
if not ((idx <> -1) and (pWebForm(Forms[idx])^.Lock = true)) then
Clearforms(Forms, form.name);
new(webf);
webf.toString := form.toString;
webf.method := form.method;
webf.name := form.name;
webf.target := form.target;
if pos('://', form.action) = 0 then begin
if copy(form.action, 1, 1) = '/' then
webf.action := doc.location.protocol + '//' + doc.location.host
+ form.action
else
webf.action := doc.location.protocol + '//' + doc.location.host
+ path + form.action;
end
else
webf.action := form.action;
webf.Lock := false;
webf.Cookies := doc.cookie;
ele := tstringlist.Create;
webf.Elements := ele;
Forms.Add(webf);
try
ElementCollection1 := form.all as IHTMLElementCollection;
for j := 0 to ElementCollection1.length - 1 do begin
Formitem := ElementCollection1.item(J, '') as IHTMLElement;
f_tagName := uppercase(Formitem.tagName);
if f_tagName = 'INPUT' then begin
InputItem := FormItem as IHTMLInputElement;
new(input_);
input_.splitThread := false;
input_.Type_ := inputitem.type_;
input_.Dict := '';
input_.Value := inputitem.value;
ele.AddObject(InputItem.name, Tobject(input_));
end;
if (f_tagName = 'SELECT') then begin
SelectItem := FormItem as IHTMLSelectElement;
new(input_);
input_.splitThread := false;
input_.Type_ := Selectitem.type_;
input_.Dict := '';
input_.Value := Selectitem.value;
ele.AddObject(SelectItem.name, Tobject(input_));
end;
//ele.Add()
end;
finally
end;
end;
end;
result := forms.count > 0;
end;

function GetFrames(Doc: IHTMLDocument2
var Frames: Tstrings): Boolean;
var
ElementCollection: IHTMLElementCollection;
HtmlElement: IHTMLElement;
I: Integer;
AnchorString: string;
frame: HTMLFrameBase;
begin
if Doc = nil then begin
result := false;
exit;
end;
//raise Exception.Create('Couldn''t convert the ' +
// 'FInternetExplorer.Document to an IHTMLDocument2');
// First, grab all the elements on the web page
ElementCollection := Doc.all;
for I := 0 to ElementCollection.length - 1 do
begin
// Get the current element
HtmlElement := ElementCollection.item(I, '') as IHTMLElement;
if HTMLElement.tagName = 'A' then
begin
AnchorString := HtmlElement.innerText;
if AnchorString = '' then
AnchorString := '(Empty Name)';
// We know that the element is an IHTMLAnchorElement since the tagName
// is 'A'.
AnchorString := AnchorString + ' - ' +
(HtmlElement as IHTMLAnchorElement).href;
// lstbxLinks.Items.Add(AnchorString);
end;
if uppercase(HTMLElement.tagName) = 'FRAME' then begin
frame := HTMLElement as HTMLFrameBase;
frames.Add(frame.getAttribute('name', 0) + '=' + frame.src);
end;
end;
result := frames.Count > 0;
end;
 
某些同志怎么上论坛了,还不开QQ啊
 
写得很乱,见谅~
 
知道说的是谁吧!开QQ接计划,我写了4小时啊
 
代码:
function trimTag(source:string):string;
var
  j,k:integer;
begin
  j:=pos('<',source);
  k:=pos('>',source);
  if j>0 and k>0 then
   begin
    result:=copy(source,1,j-1)+trimTag(copy(source,k+1,length(source)));
   end
   else
     result:=source;
end;
 
就这一段来说,想加入除错部分应该怎么写,比如http://ok567.com不存在或其他出错

procedure TForm1.Button1Click(Sender: TObject);
var
Stream: TStringStream;
begin
Stream := TStringStream.Create('');
idHttp1.Get('http://ok567.com',Stream);
Memo1.Lines.add(Stream.DataString);
Stream.Free;
end;
 
代码:
procedure TForm1.Button1Click(Sender: TObject);
var
   Stream: TStringStream;
begin
   Stream := TStringStream.Create('');
   idHttp1.Get('http://ok567.com',Stream);
   try
   case idHttp1.ResponseCode  of
     404: raise Exception.Create('404 File Not Found here Sorry')

     401: raise Exception.Create('unAuth');
       ......
   except on e:exception do ShowMessage(e.Message);
   end;
   Memo1.Lines.add(Stream.DataString);
   Stream.Free;
end;
 
呵呵,抱歉啊,昨晚有事,先下了
 
去掉<中间的内容>就是文本信息啦。呵呵,蒙的。
 

Similar threads

顶部