如何搜一个网页,将这个页面上的下拉菜单里的内容放到我的程序的ComboBox1中?(已经使用WEBBROWSER取到页面了)(50分)

  • 主题发起人 主题发起人 dzogchen
  • 开始时间 开始时间
D

dzogchen

Unregistered / Unconfirmed
GUEST, unregistred user!
如何搜一个网页,将这个页面上的下拉菜单里的内容放到我的程序的ComboBox1中?(已经使用WEBBROWSER取到页面了)
 
分析源代码:
笨办法:)
比如下面的:
<SELECT NAME="select">
<option value="">请选择</option>
<option value=itgo5_classearchType_10022007>项目资源</option>
<option value=itgo5_classearchType_10022008>技术资源</option>
<option value=itgo5_classearchType_10022009>创意资源</option>
<option value=itgo5_classearchType_10022020>学生创业</option>
<option value=itgo5_classearchType_10022021>创业动态</option>
</SELECT>
根据
<SELECT NAME="select">
</SELECT>
可得到下拉框的名称
根据
<option value=itgo5_classearchType_10022021>创业动态</option>
可得到 下拉菜单的内容
 
是这样,我就是要得到:
项目资源
技术资源
创意资源
学生创业
创业动态
 
用 POS函数 结合COPY函数 可以得到你要的东东!!!
 
使用IHTMLSelectElement接口
IHTMLSelectElement::length得到下拉的条目数目
IHTMLSelectElement::item得到条目

HRESULT GetOptions(IHTMLSelectElement *ppvSelect, BSTR *pszOptText, long *plItems)
{
IDispatch *ppvdispOption;
IHTMLOptionElement *ppvOption;
HRESULT hResult;

// Obtain the number of option objects in the select object.
ppvSelect->get_length(plItems);

for (long i=0;i<*plItems;i++){

// Get an IDispatch interface for the next option.
_variant_t index = i;
hResult = ppvSelect->item( index, index, &ppvdispOption );
if FAILED(hResult) return(hResult);

// Query for the IHTMLOptionElement interface.
hResult = ppvdispOption->QueryInterface( IID_IHTMLOptionElement,
(void **) &ppvOption);
ppvdispOption->Release();
if FAILED(hResult) return(hResult);

// Add the option text to a list box.
hResult = ppvOption->get_text(&(pszOptText));
ppvOption->Release();
if FAILED(hResult) return(hResult);
}
return S_OK;
}
 
dingbaosheng:您说的会要命的;
CathyEagle说的IHTMLSelectElement是什么呀?
 
TO:dzogchen
稍等一下 等俺弄一个贴上来
 
搞定!!!
用IE打开一个含有下拉菜单的页面
例如:
http://vc.chinanasdaq.com/cyyq/cyyq.asp?classearchTypeID=itgo5_classearchType_1002

运行本例子,点击 BUTTON1
在MEMO1里面可以得到:

索引号=0 下拉菜单名称=select 选项值=下拉框你要的值=请选择
索引号=1 下拉菜单名称=select 选项值=itgo5_classearchType_10022007下拉框你要的值=项目资源
索引号=2 下拉菜单名称=select 选项值=itgo5_classearchType_10022008下拉框你要的值=技术资源
索引号=3 下拉菜单名称=select 选项值=itgo5_classearchType_10022009下拉框你要的值=创意资源
索引号=4 下拉菜单名称=select 选项值=itgo5_classearchType_10022020下拉框你要的值=学生创业
索引号=5 下拉菜单名称=select 选项值=itgo5_classearchType_10022021下拉框你要的值=创业动态


单元文件见:http://freehost25.websamba.com/yzdbs/dispbbs.asp?boardid=7&rootid=237&id=237&star=
 
单元文件如下:
unit getIE;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OleCtrls, SHDocVw;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
procedure GetComboBoxData;
{ Public declarations }
end;

var
Form1: TForm1;

implementation
uses mshtml;
{$R *.dfm}

procedure TForm1.GetComboBoxData;
var
ShellWindow: IShellWindows;
nCount: integer;
spDisp: IDispatch;
i, j, x, xx: integer;
vi, test: OleVariant;
IE1: IWebBrowser2;
IDoc1: IHTMLDocument2;
iELC: IHTMLElementCollection;
S: string;
HtmlInputEle: IHTMLInputElement;
HtmlSelEle: IHTMLSelectElement;
Htmloption: IHTMLOptionElement;
op: Ihtmlelement;
begin
ShellWindow := CoShellWindows.Create;
nCount := ShellWindow.Count;
for i := 0 to nCount - 1 do
begin
vi := i;
spDisp := ShellWindow.Item(vi);
if spDisp = nil then continue;
spDisp.QueryInterface(iWebBrowser2, IE1);
if IE1 <> nil then
begin
IE1.Document.QueryInterface(IHTMLDocument2, iDoc1);
if iDoc1 <> nil then
begin
ielc := iDoc1.Get_all;
for j := 0 to ielc.length - 1 do
begin
Application.ProcessMessages;
spDisp := ielc.item(J, 0);
if SUCCEEDED(spDisp.QueryInterface(IHTMLselectelement, HtmlSelEle)) then
with HtmlSelEle do
begin
SelectedIndex := 0;
for x := 0 to length-1 do
begin
spDisp := HtmlSelEle.item(x, 0);
if SUCCEEDED(spDisp.QueryInterface(IHTMLOptionElement, htmloption)) then
S := S + #9 + ' 索引号=' + IntToStr(SelectedIndex)
+ ' 下拉菜单名称=' + name
+ ' 选项值=' + value+'下拉框你要的值='+htmloption.text; ;

SelectedIndex := x+1;
end;
end;
end;
Memo1.Lines.Add(S);
Exit;
end;
end;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
GetComboBoxData;
end;
end.

 
呵呵 俺给DFW 缩缩水
怎么又罐进来了 :)
 
你们都厉害呀
 
IDoc1: IHTMLDocument2;
iELC: IHTMLElementCollection;
HtmlInputEle: IHTMLInputElement;
HtmlSelEle: IHTMLSelectElement;
Htmloption: IHTMLOptionElement;
op: Ihtmlelement;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
请问dingbaosheng这都是什么呀?是控件?
 
dingbaosheng:
我按照你的办法:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OleCtrls, SHDocVw;

type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
WebBrowser1: TWebBrowser;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure GetComboBoxData;
end;

var
Form1: TForm1;

implementation
uses mshtml;
{$R *.dfm}
procedure TForm1.GetComboBoxData;
var
ShellWindow: IShellWindows;
nCount: integer;
spDisp: IDispatch;
i, j, x, xx: integer;
vi, test: OleVariant;
IE1: IWebBrowser2;
IDoc1: IHTMLDocument2;
iELC: IHTMLElementCollection;
S: string;
HtmlInputEle: IHTMLInputElement;
HtmlSelEle: IHTMLSelectElement;
Htmloption: IHTMLOptionElement;
op: Ihtmlelement;
begin
ShellWindow := CoShellWindows.Create;
nCount := ShellWindow.Count;
for i := 0 to nCount - 1 do
begin
vi := i;
spDisp := ShellWindow.Item(vi);
if spDisp = nil then continue;
spDisp.QueryInterface(iWebBrowser2, IE1);
if IE1 <> nil then
begin
IE1.Document.QueryInterface(IHTMLDocument2, iDoc1);
if iDoc1 <> nil then
begin
ielc := iDoc1.Get_all;
for j := 0 to ielc.length - 1 do
begin
Application.ProcessMessages;
spDisp := ielc.item(J, 0);
if SUCCEEDED(spDisp.QueryInterface(IHTMLselectelement, HtmlSelEle)) then
with HtmlSelEle do
begin
SelectedIndex := 0;
for x := 0 to length-1 do
begin
spDisp := HtmlSelEle.item(x, 0);
if SUCCEEDED(spDisp.QueryInterface(IHTMLOptionElement, htmloption)) then
S := S + #9 + ' Ë÷ÒýºÅ=' + IntToStr(SelectedIndex)
+ ' ÏÂÀ­²Ëµ¥Ãû³Æ=' + name
+ ' Ñ¡ÏîÖµ=' + value+'ÏÂÀ­¿òÄãÒªµÄÖµ='+htmloption.text; ;

SelectedIndex := x+1;
end;
end;
end;
Memo1.Lines.Add(S);
Exit;
end;
end;
end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
WebBrowser1.Navigate('http://vc.chinanasdaq.com/cyyq/cyyq.asp?classearchTypeID=itgo5_classearchType_1002');
GetComboBoxData;
end;

end.



1、现在的问题是,可以在WebBrowser1看见'http://vc.chinanasdaq.com/cyyq/cyyq.asp?classearchTypeID=itgo5_classearchType_1002的页面,但是MEMO1中什么都没有。;
2、IDoc1: IHTMLDocument2; iELC: IHTMLElementCollection;HtmlInputEle: IHTMLInputElement; HtmlSelEle: IHTMLSelectElement; Htmloption: IHTMLOptionElement; op: Ihtmlelement;
都是什么?
3、IE1: IWebBrowser2; IDoc1: IHTMLDocument2;是什么?我这里只有TWebBrowser1呀?
 
原来是要一个HttpCli1控件。
 
接受答案了.
 
后退
顶部