Delphi webbrowser-如何从id内的标记名中获取值
我正在使用Delphi7并制作一个小程序,在这个程序中,我使用webbrowser从id内的标记中获取值 下面是保存值的html示例Delphi webbrowser-如何从id内的标记名中获取值,delphi,Delphi,我正在使用Delphi7并制作一个小程序,在这个程序中,我使用webbrowser从id内的标记中获取值 下面是保存值的html示例 <div id="download" style="display: block;"> <a style="display:none" href="blablabla"></a> <a href="Need This value"></a> <a style="displa
<div id="download" style="display: block;">
<a style="display:none" href="blablabla"></a>
<a href="Need This value"></a>
<a style="display:none"blablabla"></a>
<a style="display:none"blablabla"></a>
<span style="margin-left:8px; color:#808080; overflow:hidden; white-space: nowrap"></span>
</div>
我可以获得id为“下载”的innerHTML
我的问题是如何从标记“a”中获取“href”的值。我不知道这是否是最佳解决方案,但它在这里起作用:
function FindYourValue(block : string) : string;
var text, subtext : string;
quant : integer;
begin
text := block;
if pos('<a href="',text) > 0 then
subtext := copy(text,pos('<a href="',text) + 9,length(text));
quant := pos('"',subtext);
result := copy(subtext,1,9);
end;
函数FindYourValue(block:string):string;
变量文本,子文本:字符串;
数量:整数;
开始
文本:=块;
if pos(“不知道这是否是最好的解决方案,但它在这里起作用:
function FindYourValue(block : string) : string;
var text, subtext : string;
quant : integer;
begin
text := block;
if pos('<a href="',text) > 0 then
subtext := copy(text,pos('<a href="',text) + 9,length(text));
quant := pos('"',subtext);
result := copy(subtext,1,9);
end;
函数FindYourValue(block:string):string;
变量文本,子文本:字符串;
数量:整数;
开始
文本:=块;
if pos(“不知道这是否是最好的解决方案,但它在这里起作用:
function FindYourValue(block : string) : string;
var text, subtext : string;
quant : integer;
begin
text := block;
if pos('<a href="',text) > 0 then
subtext := copy(text,pos('<a href="',text) + 9,length(text));
quant := pos('"',subtext);
result := copy(subtext,1,9);
end;
函数FindYourValue(block:string):string;
变量文本,子文本:字符串;
数量:整数;
开始
文本:=块;
if pos(“不知道这是否是最好的解决方案,但它在这里起作用:
function FindYourValue(block : string) : string;
var text, subtext : string;
quant : integer;
begin
text := block;
if pos('<a href="',text) > 0 then
subtext := copy(text,pos('<a href="',text) + 9,length(text));
quant := pos('"',subtext);
result := copy(subtext,1,9);
end;
函数FindYourValue(block:string):string;
变量文本,子文本:字符串;
数量:整数;
开始
文本:=块;
如果pos(“如果所讨论的
标记有名称
或id
分配给它,您可以简单地使用IHTMLDocument2.archors
集合来查找它,例如:
var
Doc: IHTMLDocument2;
Anchor: IHTMLAnchorElement;
Value: String;
begin
Doc := WebBrowser1.Document as IHTMLDocument2;
Anchor := Doc.anchors.item('name', 0) as IHTMLAnchorElement;
Value := Anchor.href;
end;
var
Doc: IHTMLDocument3;
Download: IHTMLElement;
Coll: IHTMLElementCollection;
Anchor: IHTMLAnchorElement;
Value: String;
begin
Doc := WebBrowser1.Document as IHTMLDocument3;
Download := Doc.getElementById('download') as IHTMLElement;
Coll := Download.children as IHTMLElementCollection;
Coll := Coll.tags('a') as IHTMLElementCollection;
Anchor := Coll.item(1, 0) as IHTMLAnchorElement;
Value := Anchor.href;
end;
但是,由于它没有名称
或id
,因此必须进一步挖掘DOM,例如:
var
Doc: IHTMLDocument2;
Anchor: IHTMLAnchorElement;
Value: String;
begin
Doc := WebBrowser1.Document as IHTMLDocument2;
Anchor := Doc.anchors.item('name', 0) as IHTMLAnchorElement;
Value := Anchor.href;
end;
var
Doc: IHTMLDocument3;
Download: IHTMLElement;
Coll: IHTMLElementCollection;
Anchor: IHTMLAnchorElement;
Value: String;
begin
Doc := WebBrowser1.Document as IHTMLDocument3;
Download := Doc.getElementById('download') as IHTMLElement;
Coll := Download.children as IHTMLElementCollection;
Coll := Coll.tags('a') as IHTMLElementCollection;
Anchor := Coll.item(1, 0) as IHTMLAnchorElement;
Value := Anchor.href;
end;
如果所讨论的
标记有名称
或id
分配给它,您可以简单地使用IHTMLDocument2.archors
集合来查找它,例如:
var
Doc: IHTMLDocument2;
Anchor: IHTMLAnchorElement;
Value: String;
begin
Doc := WebBrowser1.Document as IHTMLDocument2;
Anchor := Doc.anchors.item('name', 0) as IHTMLAnchorElement;
Value := Anchor.href;
end;
var
Doc: IHTMLDocument3;
Download: IHTMLElement;
Coll: IHTMLElementCollection;
Anchor: IHTMLAnchorElement;
Value: String;
begin
Doc := WebBrowser1.Document as IHTMLDocument3;
Download := Doc.getElementById('download') as IHTMLElement;
Coll := Download.children as IHTMLElementCollection;
Coll := Coll.tags('a') as IHTMLElementCollection;
Anchor := Coll.item(1, 0) as IHTMLAnchorElement;
Value := Anchor.href;
end;
但是,由于它没有名称
或id
,因此必须进一步挖掘DOM,例如:
var
Doc: IHTMLDocument2;
Anchor: IHTMLAnchorElement;
Value: String;
begin
Doc := WebBrowser1.Document as IHTMLDocument2;
Anchor := Doc.anchors.item('name', 0) as IHTMLAnchorElement;
Value := Anchor.href;
end;
var
Doc: IHTMLDocument3;
Download: IHTMLElement;
Coll: IHTMLElementCollection;
Anchor: IHTMLAnchorElement;
Value: String;
begin
Doc := WebBrowser1.Document as IHTMLDocument3;
Download := Doc.getElementById('download') as IHTMLElement;
Coll := Download.children as IHTMLElementCollection;
Coll := Coll.tags('a') as IHTMLElementCollection;
Anchor := Coll.item(1, 0) as IHTMLAnchorElement;
Value := Anchor.href;
end;
如果所讨论的
标记有名称
或id
分配给它,您可以简单地使用IHTMLDocument2.archors
集合来查找它,例如:
var
Doc: IHTMLDocument2;
Anchor: IHTMLAnchorElement;
Value: String;
begin
Doc := WebBrowser1.Document as IHTMLDocument2;
Anchor := Doc.anchors.item('name', 0) as IHTMLAnchorElement;
Value := Anchor.href;
end;
var
Doc: IHTMLDocument3;
Download: IHTMLElement;
Coll: IHTMLElementCollection;
Anchor: IHTMLAnchorElement;
Value: String;
begin
Doc := WebBrowser1.Document as IHTMLDocument3;
Download := Doc.getElementById('download') as IHTMLElement;
Coll := Download.children as IHTMLElementCollection;
Coll := Coll.tags('a') as IHTMLElementCollection;
Anchor := Coll.item(1, 0) as IHTMLAnchorElement;
Value := Anchor.href;
end;
但是,由于它没有名称
或id
,因此必须进一步挖掘DOM,例如:
var
Doc: IHTMLDocument2;
Anchor: IHTMLAnchorElement;
Value: String;
begin
Doc := WebBrowser1.Document as IHTMLDocument2;
Anchor := Doc.anchors.item('name', 0) as IHTMLAnchorElement;
Value := Anchor.href;
end;
var
Doc: IHTMLDocument3;
Download: IHTMLElement;
Coll: IHTMLElementCollection;
Anchor: IHTMLAnchorElement;
Value: String;
begin
Doc := WebBrowser1.Document as IHTMLDocument3;
Download := Doc.getElementById('download') as IHTMLElement;
Coll := Download.children as IHTMLElementCollection;
Coll := Coll.tags('a') as IHTMLElementCollection;
Anchor := Coll.item(1, 0) as IHTMLAnchorElement;
Value := Anchor.href;
end;
如果所讨论的
标记有名称
或id
分配给它,您可以简单地使用IHTMLDocument2.archors
集合来查找它,例如:
var
Doc: IHTMLDocument2;
Anchor: IHTMLAnchorElement;
Value: String;
begin
Doc := WebBrowser1.Document as IHTMLDocument2;
Anchor := Doc.anchors.item('name', 0) as IHTMLAnchorElement;
Value := Anchor.href;
end;
var
Doc: IHTMLDocument3;
Download: IHTMLElement;
Coll: IHTMLElementCollection;
Anchor: IHTMLAnchorElement;
Value: String;
begin
Doc := WebBrowser1.Document as IHTMLDocument3;
Download := Doc.getElementById('download') as IHTMLElement;
Coll := Download.children as IHTMLElementCollection;
Coll := Coll.tags('a') as IHTMLElementCollection;
Anchor := Coll.item(1, 0) as IHTMLAnchorElement;
Value := Anchor.href;
end;
但是,由于它没有名称
或id
,因此必须进一步挖掘DOM,例如:
var
Doc: IHTMLDocument2;
Anchor: IHTMLAnchorElement;
Value: String;
begin
Doc := WebBrowser1.Document as IHTMLDocument2;
Anchor := Doc.anchors.item('name', 0) as IHTMLAnchorElement;
Value := Anchor.href;
end;
var
Doc: IHTMLDocument3;
Download: IHTMLElement;
Coll: IHTMLElementCollection;
Anchor: IHTMLAnchorElement;
Value: String;
begin
Doc := WebBrowser1.Document as IHTMLDocument3;
Download := Doc.getElementById('download') as IHTMLElement;
Coll := Download.children as IHTMLElementCollection;
Coll := Coll.tags('a') as IHTMLElementCollection;
Anchor := Coll.item(1, 0) as IHTMLAnchorElement;
Value := Anchor.href;
end;
Remy Lebeau@RemyLebeau提供的解决方案应该有效。但如果HTML不包含您期望的元素,您可能会遇到异常。如果您希望以更安全的方式编写代码,我建议您使用支持
来查询HTML元素接口
function ExtractUrlExample: TStringList;
var
Doc: IHTMLDocument3;
Download: IHTMLElement;
Coll: IHTMLElementCollection;
I: Integer;
Anchor: IHTMLAnchorElement;
begin
Result := TStringList.Create;
if Supports(WebBrowser1.Document, IHTMLDocument3, Doc) and
Supports(Doc.getElementById('download'), IHTMLElement, Download) and
Supports(Download.children, IHTMLElementCollection, Coll) and
Supports(Coll.tags('a'), IHTMLElementCollection, Coll) then
begin
for I := 0 to Coll.Length - 1 do
if Supports(Coll.item(I, 0), IHTMLAnchorElement, Anchor) then
Result.Add(Anchor.href); // The Url you want to extract
end;
end;
Remy Lebeau@RemyLebeau提供的解决方案应该有效。但如果HTML不包含您期望的元素,您可能会遇到异常。如果您希望以更安全的方式编写代码,我建议您使用支持
来查询HTML元素接口
function ExtractUrlExample: TStringList;
var
Doc: IHTMLDocument3;
Download: IHTMLElement;
Coll: IHTMLElementCollection;
I: Integer;
Anchor: IHTMLAnchorElement;
begin
Result := TStringList.Create;
if Supports(WebBrowser1.Document, IHTMLDocument3, Doc) and
Supports(Doc.getElementById('download'), IHTMLElement, Download) and
Supports(Download.children, IHTMLElementCollection, Coll) and
Supports(Coll.tags('a'), IHTMLElementCollection, Coll) then
begin
for I := 0 to Coll.Length - 1 do
if Supports(Coll.item(I, 0), IHTMLAnchorElement, Anchor) then
Result.Add(Anchor.href); // The Url you want to extract
end;
end;
Remy Lebeau@RemyLebeau提供的解决方案应该有效。但如果HTML不包含您期望的元素,您可能会遇到异常。如果您希望以更安全的方式编写代码,我建议您使用支持
来查询HTML元素接口
function ExtractUrlExample: TStringList;
var
Doc: IHTMLDocument3;
Download: IHTMLElement;
Coll: IHTMLElementCollection;
I: Integer;
Anchor: IHTMLAnchorElement;
begin
Result := TStringList.Create;
if Supports(WebBrowser1.Document, IHTMLDocument3, Doc) and
Supports(Doc.getElementById('download'), IHTMLElement, Download) and
Supports(Download.children, IHTMLElementCollection, Coll) and
Supports(Coll.tags('a'), IHTMLElementCollection, Coll) then
begin
for I := 0 to Coll.Length - 1 do
if Supports(Coll.item(I, 0), IHTMLAnchorElement, Anchor) then
Result.Add(Anchor.href); // The Url you want to extract
end;
end;
Remy Lebeau@RemyLebeau提供的解决方案应该有效。但如果HTML不包含您期望的元素,您可能会遇到异常。如果您希望以更安全的方式编写代码,我建议您使用支持
来查询HTML元素接口
function ExtractUrlExample: TStringList;
var
Doc: IHTMLDocument3;
Download: IHTMLElement;
Coll: IHTMLElementCollection;
I: Integer;
Anchor: IHTMLAnchorElement;
begin
Result := TStringList.Create;
if Supports(WebBrowser1.Document, IHTMLDocument3, Doc) and
Supports(Doc.getElementById('download'), IHTMLElement, Download) and
Supports(Download.children, IHTMLElementCollection, Coll) and
Supports(Coll.tags('a'), IHTMLElementCollection, Coll) then
begin
for I := 0 to Coll.Length - 1 do
if Supports(Coll.item(I, 0), IHTMLAnchorElement, Anchor) then
Result.Add(Anchor.href); // The Url you want to extract
end;
end;
你能给我们展示你的代码你是如何获得下载div的吗?你能给我们展示你的代码你是如何获得下载div的吗?你能给我们展示你的代码你是如何获得下载div的吗?你能给我们展示你的代码你是如何获得下载div的吗id如您所见,共有4个标记您的代码向我显示第一个标记的值我需要第二个标记的值thx用于您的时间OK抱歉被锚最终愚蠢地解决:=Coll.item(2,3)作为IHTMlanchoreElement;thx再次Remy lebeau(Y)IHTMlementCollection
为0索引。item(1,0)
返回第二个项目,项目(2,0)
返回第三个项目,等等。您说过您想要第二个的值。您好,thx这是一个很好的例子,它可以工作。完美的标签在“下载”中id如您所见,共有4个标记您的代码向我显示第一个标记的值我需要第二个标记的值thx用于您的时间OK抱歉被锚最终愚蠢地解决:=Coll.item(2,3)作为IHTMlanchoreElement;thx再次Remy lebeau(Y)IHTMlementCollection
为0索引。item(1,0)
返回第二个项目,项目(2,0)
返回第三个项目,等等。您说过您想要第二个的值。您好,thx这是一个很好的例子,它可以工作。完美的标签在“下载”中id如您所见,共有4个标记您的代码向我显示第一个标记的值我需要第二个标记的值thx用于您的时间OK抱歉被锚最终愚蠢地解决:=Coll.item(2,3)作为IHTMlanchoreElement;thx再次Remy lebeau(Y)IHTMlementCollection
为0索引。item(1,0)
返回第二个项目,项目(2,0)
返回第三个项目,等等。您说过您想要第二个的值。您好,thx这是一个很好的例子,它可以工作。完美的标签在“下载”中id如您所见,共有4个标记您的代码向我显示第一个标记的值我需要第二个标记的值thx用于您的时间OK抱歉被锚最终愚蠢地解决:=Coll.item(2,3)作为IHTMlanchoreElement;thx再次Remy lebeau(Y)IHTMlementCollection
为0索引。item(1,0)
返回第二项,项(2,0)
retu