Delphi自动提交网页表单和获取框架网页源码
2013-12-10 13:24
501 查看
这两个问题的实现原理其实是差不多的,所以放在一起介绍,单元MSHtml封装了我们需要的功能。
首先,新建一个DELPHI工程,在USES部分添加MSHtml单元的引用。
然后,在窗体上放置一个TWebBrowser控件和四个按钮。
最后,编写四个按钮的响应代码:
1. 自动提交网页表单
procedure TForm1.Button1Click(Sender: TObject);
begin
WebBrowser1.Navigate('http://www.baidu.com');
end;
procedure TForm1.Button2Click(Sender: TObject);
var
doc: IHTMLDocument2;
oleObj: OleVariant;
begin
doc := WebBrowser1.Document as IHTMLDocument2;
if doc = nil then Exit;
oleObj := doc.all.item('wd', 0) as IHTMLElement2;
//网页有一个名为“wd”的文本框:<input type="text" name="wd"
id="kw" maxlength="100">
oleObj.Value := 'Delphi'; //为文本框赋值
oleObj := doc.all.item('su', 0) as IHTMLElement2;
//网页有一个ID为“su”的按钮:<input type="submit" value="百度一下" id="su">
oleObj.Click; //点击按钮,提交表单
end;
2. 获取框架网页源码
procedure TForm1.Button3Click(Sender: TObject);
begin
WebBrowser1.Navigate('http://含有框架的网页URL');
end;
procedure TForm1.Button4Click(Sender: TObject);
var
doc, framedoc: IHTMLDocument2;
frame_dispatch: IDispatch;
ole_index: OleVariant;
i: Integer;
begin
doc := WebBrowser1.Document as IHTMLDocument2;
if doc = nil then Exit;
for i := 0 to doc.frames.length
- 1 do
begin
ole_index := i;
frame_dispatch := doc.frames.item(ole_index);
if frame_dispatch = nil then Continue;
framedoc := (frame_dispatch as IHTMLWindow2).document;
if framedoc = nil then Continue;
ShowMessage(framedoc.body.innerHTML);
end;
end;
3. 获取网页所有链接
procedure TForm1.Button1Click(Sender: TObject);
var
elem: IHTMLElement;
coll: IHTMLElementCollection;
i: integer;
url, title: string;
begin
coll := (WebBrowser1.Document as IHTMLDocument2).all;
coll := (coll.tags('a') as IHTMLElementCollection);
for i := 0 to coll.Length - 1 do
begin // 循环取出每个链接
elem := (coll.item(i, 0) as IHTMLElement);
url := Trim(string(elem.getAttribute(WideString('href'), 0)));
title := elem.innerText;
ShowMessage(Format('链接标题:%s,链接网址:%s', [title, url]));
end;
end;
首先,新建一个DELPHI工程,在USES部分添加MSHtml单元的引用。
然后,在窗体上放置一个TWebBrowser控件和四个按钮。
最后,编写四个按钮的响应代码:
1. 自动提交网页表单
procedure TForm1.Button1Click(Sender: TObject);
begin
WebBrowser1.Navigate('http://www.baidu.com');
end;
procedure TForm1.Button2Click(Sender: TObject);
var
doc: IHTMLDocument2;
oleObj: OleVariant;
begin
doc := WebBrowser1.Document as IHTMLDocument2;
if doc = nil then Exit;
oleObj := doc.all.item('wd', 0) as IHTMLElement2;
//网页有一个名为“wd”的文本框:<input type="text" name="wd"
id="kw" maxlength="100">
oleObj.Value := 'Delphi'; //为文本框赋值
oleObj := doc.all.item('su', 0) as IHTMLElement2;
//网页有一个ID为“su”的按钮:<input type="submit" value="百度一下" id="su">
oleObj.Click; //点击按钮,提交表单
end;
2. 获取框架网页源码
procedure TForm1.Button3Click(Sender: TObject);
begin
WebBrowser1.Navigate('http://含有框架的网页URL');
end;
procedure TForm1.Button4Click(Sender: TObject);
var
doc, framedoc: IHTMLDocument2;
frame_dispatch: IDispatch;
ole_index: OleVariant;
i: Integer;
begin
doc := WebBrowser1.Document as IHTMLDocument2;
if doc = nil then Exit;
for i := 0 to doc.frames.length
- 1 do
begin
ole_index := i;
frame_dispatch := doc.frames.item(ole_index);
if frame_dispatch = nil then Continue;
framedoc := (frame_dispatch as IHTMLWindow2).document;
if framedoc = nil then Continue;
ShowMessage(framedoc.body.innerHTML);
end;
end;
3. 获取网页所有链接
procedure TForm1.Button1Click(Sender: TObject);
var
elem: IHTMLElement;
coll: IHTMLElementCollection;
i: integer;
url, title: string;
begin
coll := (WebBrowser1.Document as IHTMLDocument2).all;
coll := (coll.tags('a') as IHTMLElementCollection);
for i := 0 to coll.Length - 1 do
begin // 循环取出每个链接
elem := (coll.item(i, 0) as IHTMLElement);
url := Trim(string(elem.getAttribute(WideString('href'), 0)));
title := elem.innerText;
ShowMessage(Format('链接标题:%s,链接网址:%s', [title, url]));
end;
end;
相关文章推荐
- 设计自动获取网页和提交表单组件
- Delphi实现网页表单数据的自动提交
- 关于c#的webbrows控件自动填写表单并获取提交后的网页内容
- 设计自动获取网页和提交表单组件
- 设计自动获取网页和提交表单组件
- Delphi实现网页表单数据的自动提交
- Delphi实现网页表单数据的自动提交
- webBroswer自动提交表单 webBroser过滤某些标签元素 webBroswer截取网页部分内容 webBroswer 获取 iframe里的元素
- Delphi实现网页表单数据的自动提交
- Delphi实现网页表单数据的自动提交
- 忆龙2009:用C#如何自动提交网页表单
- iOS 中使用JS提取网页数据,自动提交表单等
- C#获取网页源码,自动判断网页字符集编码[转]
- android 获取当前webview显示的网页的源码和网页中框架的源码
- C#获取网页源码,自动判断网页字符集编码[转]
- 网页中表单按回车就自动提交的问题的解决方案
- VC提交网页表单-自动评论留言(2)
- 如何将网页表单中提交的信息自动发送到指定邮箱?
- c# 获取网页源码,自动判断编码格式新方法!
- 忆龙2009:用C#如何自动提交网页表单