Delphi与网页的交互问题!!!!! 有没有API或者其他方法,实现直接从浏览器中获取到网页信息,然后进行相应的数据操作!求指点 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 请问如果用IdHTTP进行GET()和POST()的话,在浏览器中已经被打开的网页是否能同步更新到 在浏览器中已经被打开的网页 必须 自动或程序操作地 刷新一下不使用webbrowser,关键是无法得到js执行后的效果 找个WireShark抓包。不开WEBBROWSER 用Indy模拟,json,js什么都搞定。 可以使用idhttp来处理,你下载个HttpAnalyzerStdV6抓下包,过滤出来有效的http步骤,然后模拟就好了 function TfrmMain.GetXXXXXXXXXStatus(const xh: string; out xx, zylb, qdh, zysj, ch, hph, fz, dz, zt: string): Boolean;var PostParams :TStrings; s ,Html:string; Match :TMatch; IndyHttp :TIdHttp; IndyCookieManager :TIdCookieManager; IndyZlib: TIdCompressorZLib;begin xx :=''; zylb :=''; qdh :=''; zysj :=''; ch :=''; hph :=''; fz :=''; dz :=''; zt :=''; Randomize; Result :=True; PostParams :=TStringList.Create; IndyHttp :=TIdhttp.Create(nil); IndyCookieManager :=TIdCookieManager.Create(nil); IndyZlib :=TIdCompressorZLib.Create(nil); try try //配置参数 IndyHttp.CookieManager :=IndyCookieManager; IndyHttp.Compressor :=IndyZlib; IndyHttp.Disconnect; IndyHttp.HandleRedirects :=False; IndyHttp.AllowCookies :=True; IndyHttp.Request.CustomHeaders.Clear; IndyHttp.HTTPOptions :=[hoForceEncodeParams]; IndyHttp.Request.Accept :='text/html, application/xhtml+xml, */*'; IndyHttp.Request.AcceptEncoding :=''; IndyHttp.Request.AcceptLanguage :='zh-CN'; IndyHttp.Request.CacheControl :='no-cache'; IndyHttp.Request.Connection :='Keep-Alive'; IndyHttp.Request.UserAgent :='Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)'; IndyHttp.Request.Host :='XXXXXXXXXXXXXXXXXX'; IndyHttp.Request.Referer :='XXXXXXXXXXXXXXXXXXXXXXXX.jsp'; IndyHttp.Request.ContentType :='application/x-www-form-urlencoded'; PostParams.Add('number1='+xh); PostParams.Add('number2='); PostParams.Add('number3='); PostParams.Add('number4='); PostParams.Add('number5='); PostParams.Add('Submit=确定'); Html :=IndyHttp.Post('http://XXXXXXXXXXXXXXXXXX.jsp',PostParams); //sl.SaveToFile('D:\111.html'); //sl.LoadFromFile('D:\111.html'); s :=Copy(xh,1,4); s :=s + ' ' +Copy(xh,5,7); s := s+ '(?:.*\r?\n){4}.*?>(.*?) *\r?\n'; s := s+ '(?:.*\r?\n){5} *(.*?) *\r?\n'; s := s+ '(?:.*\r?\n){4}.*?>(.*?) *\r?\n'; s := s+ '(?:.*\r?\n){5} *(.*?) *\r?\n'; s := s+ '(?:.*\r?\n){3}.*?>(.*?) *\r?\n'; s := s+ '(?:.*\r?\n){3}.*?>(.*?) *\r?\n'; s := s+ '(?:.*\r?\n){5} *(.*?) *\r?\n'; s := s+ '(?:.*\r?\n){4} *(.*?) *\r?\n'; s := s+ '(?:.*\r?\n){4}.*?>(.*?) *\r?\n'; Match :=TRegEx.Match(Html,s,[roMultiLine]); if Match.Success then begin xx :=Match.Groups[1].Value; zylb :=Match.Groups[2].Value; qdh :=Match.Groups[3].Value; zysj :=Match.Groups[4].Value; ch :=Match.Groups[5].Value; hph :=Match.Groups[6].Value; fz :=Match.Groups[7].Value; dz :=Match.Groups[8].Value; zt :=Match.Groups[9].Value; end; Delay(Random(2000)+1000); except Result :=False; end; finally PostParams.Free; IndyHttp.Disconnect; IndyHttp.Free; IndyCookieManager.Free; IndyZlib.Free; end;end; 不是非用WebBrowser才得到内容。可以从IE中直接获取窗口Handle,然后获取Com+ Shell.Application获取IWebBrowser接口,就可以直接操作,js执行后的结果同样能得到。 自己写很难哦,你能自己写浏览器内核了。你找不到需要填写数据的地方,或者是一个textbox或者是一个combbox.WebBrowser使用的是IE的内核,你看看源代码,只引用写了多少行?自动网页填充数据也必须是在网页已经打开的情况下完成的。 怎么没有人提到IHTMLDocument2呢?虽然执行效率比idhttp低,但很实用。 现在就是用的IHTMLDocument2,先是通过CreateOleObject创建一个对象实现的! var IE: Variant; Doc:IHTMLDocument2; InputEle: IHTMLInputElement;begin IE := CreateOleObject('InternetExplorer.Application'); IE.Visible := True; IE.Navigate('C:\Test.html'); Doc := (IDispatch(IE.document) as IHTMLDocument2); if Doc=nil then exit; InputEle:=(Doc.all.item('A204098',0) as IHTMLInputElement); InputEle.Value := 'TestText';end;最后实现处理 用Delphi 向Execel导数据时,如何控制Execel 的单元格为文本格式? 求汽修厂管理软件一套,流程图加设计方案,我的QQ:35606156,在线 如何枚举出系统中的所有串口 BORdbk70.dll,奶奶的,我快疯了! 我很犹豫,学过半年的VB,想转DELPHI,请高手给点意见。 如何用Form2将Form1的DBGird中的数据打印出来? 如何使某一个窗体在程序运行时第一个显示? 关于感情的问题…… 招程序员,广东省普宁市区请进! 何会提示field未定义? delphi dbgrid 多层表头 ocx 与dax error
不开WEBBROWSER 用Indy模拟,json,js什么都搞定。
ch, hph, fz, dz, zt: string): Boolean;
var
PostParams :TStrings;
s ,Html:string;
Match :TMatch;
IndyHttp :TIdHttp;
IndyCookieManager :TIdCookieManager;
IndyZlib: TIdCompressorZLib;
begin
xx :='';
zylb :='';
qdh :='';
zysj :='';
ch :='';
hph :='';
fz :='';
dz :='';
zt :='';
Randomize;
Result :=True;
PostParams :=TStringList.Create; IndyHttp :=TIdhttp.Create(nil);
IndyCookieManager :=TIdCookieManager.Create(nil);
IndyZlib :=TIdCompressorZLib.Create(nil);
try
try
//配置参数
IndyHttp.CookieManager :=IndyCookieManager;
IndyHttp.Compressor :=IndyZlib;
IndyHttp.Disconnect;
IndyHttp.HandleRedirects :=False;
IndyHttp.AllowCookies :=True;
IndyHttp.Request.CustomHeaders.Clear;
IndyHttp.HTTPOptions :=[hoForceEncodeParams];
IndyHttp.Request.Accept :='text/html, application/xhtml+xml, */*';
IndyHttp.Request.AcceptEncoding :='';
IndyHttp.Request.AcceptLanguage :='zh-CN';
IndyHttp.Request.CacheControl :='no-cache';
IndyHttp.Request.Connection :='Keep-Alive';
IndyHttp.Request.UserAgent :='Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)'; IndyHttp.Request.Host :='XXXXXXXXXXXXXXXXXX';
IndyHttp.Request.Referer :='XXXXXXXXXXXXXXXXXXXXXXXX.jsp';
IndyHttp.Request.ContentType :='application/x-www-form-urlencoded'; PostParams.Add('number1='+xh);
PostParams.Add('number2=');
PostParams.Add('number3=');
PostParams.Add('number4=');
PostParams.Add('number5=');
PostParams.Add('Submit=确定');
Html :=IndyHttp.Post('http://XXXXXXXXXXXXXXXXXX.jsp',PostParams);
//sl.SaveToFile('D:\111.html');
//sl.LoadFromFile('D:\111.html');
s :=Copy(xh,1,4);
s :=s + ' ' +Copy(xh,5,7);
s := s+ '(?:.*\r?\n){4}.*?>(.*?) *\r?\n';
s := s+ '(?:.*\r?\n){5} *(.*?) *\r?\n';
s := s+ '(?:.*\r?\n){4}.*?>(.*?) *\r?\n';
s := s+ '(?:.*\r?\n){5} *(.*?) *\r?\n';
s := s+ '(?:.*\r?\n){3}.*?>(.*?) *\r?\n';
s := s+ '(?:.*\r?\n){3}.*?>(.*?) *\r?\n';
s := s+ '(?:.*\r?\n){5} *(.*?) *\r?\n';
s := s+ '(?:.*\r?\n){4} *(.*?) *\r?\n';
s := s+ '(?:.*\r?\n){4}.*?>(.*?) *\r?\n';
Match :=TRegEx.Match(Html,s,[roMultiLine]);
if Match.Success then
begin
xx :=Match.Groups[1].Value;
zylb :=Match.Groups[2].Value;
qdh :=Match.Groups[3].Value;
zysj :=Match.Groups[4].Value;
ch :=Match.Groups[5].Value;
hph :=Match.Groups[6].Value;
fz :=Match.Groups[7].Value;
dz :=Match.Groups[8].Value;
zt :=Match.Groups[9].Value;
end;
Delay(Random(2000)+1000);
except
Result :=False;
end;
finally
PostParams.Free;
IndyHttp.Disconnect;
IndyHttp.Free;
IndyCookieManager.Free;
IndyZlib.Free;
end;
end;
你找不到需要填写数据的地方,或者是一个textbox或者是一个combbox.
WebBrowser使用的是IE的内核,你看看源代码,只引用写了多少行?
自动网页填充数据也必须是在网页已经打开的情况下完成的。
var
IE: Variant;
Doc:IHTMLDocument2;
InputEle: IHTMLInputElement;begin
IE := CreateOleObject('InternetExplorer.Application');
IE.Visible := True;
IE.Navigate('C:\Test.html'); Doc := (IDispatch(IE.document) as IHTMLDocument2);
if Doc=nil then exit;
InputEle:=(Doc.all.item('A204098',0) as IHTMLInputElement);
InputEle.Value := 'TestText';
end;
最后实现处理