简体   繁体   English

Delphi webbrowser - 如何从id中的标记名获取值

[英]Delphi webbrowser - How to Get value from tag name inside id

i am using delphi 7 and making small program in which i use webbrowser to get value from tag inside id 我正在使用delphi 7并制作小程序,其中我使用webbrowser从id中的标签获取值

here is html example of where value is saved 这是保存值的html示例

<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>

i can get innerHTML of id "download" my problem is after how to get value of "href" from tag "a" 我可以获得id“下载”的innerHTML我的问题是如何从标签“a”获得“href”的值

If the <a> tag in question had a name or id assigned to it, you could have simply used the IHTMLDocument2.anchors collection to find it, for example: 如果有问题的<a>标签分配了nameid ,您可以简单地使用IHTMLDocument2.anchors集合来查找它,例如:

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;

But, since it does not have a name or id , you will have to dig through the DOM a little bit further, for example: 但是,由于它没有nameid ,因此您需要进一步深入挖掘DOM,例如:

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;

Don't know if it's the best solution, but it worked here: 不知道它是否是最好的解决方案,但它在这里工作:

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;

Where BLOCK is the string you get from the <div id="download" style="display: block;"> routine. 其中BLOCK是从<div id="download" style="display: block;">例程获得的字符串。

The solution provided by Remy Lebeau @RemyLebeau should work. Remy Lebeau @RemyLebeau提供的解决方案应该可行。 But you might meet exception, if the HTML does not contain the element you are expecting. 但是,如果HTML不包含您期望的元素,则可能会遇到异常。 If you want to write your code in a more secure way, I'd suggest you use Supports to query the Html element interfaces. 如果您想以更安全的方式编写代码,我建议您使用Supports来查询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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM