查找特定的HTML数据和HtmlElement元素(集合)和网页浏览器 [英] Find specific data in html with HtmlElement(Collection) and webbrowser

查看:369
本文介绍了查找特定的HTML数据和HtmlElement元素(集合)和网页浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到与类名XYZ一个div然后它,我想通过一个名为ABC一堆元素的循环。然后抓住内部的链接(A HREF)和可能的其他信息。

I want to find a div with the class name XYZ then in it I want to loop through a bunch of elements named ABC. Then grab the links (a href) inside and possibly other information.

我如何找到XYZ股利从 webBrowser1.Document.Links 和所有子项我想要什么?

How do I find the div with XYZ from webBrowser1.Document.Links and any subitems I want?

推荐答案

首先,你说你要找到与类名XYZ一个div,为什么你在看webBrowser1.Documnet.Links?先找到事业部,然后得到其中的链接。

First you said you want to find a div with the class name XYZ, so why are you looking in webBrowser1.Documnet.Links? Find the Div first, then get to the links within it.

HtmlDocument doc = webBrowser.Document;
HtmlElementCollection col = doc.GetElementsByTagName("div");
foreach (HtmlElement element in col)
{
    string cls = element.GetAttribute("className");
    if (String.IsNullOrEmpty(cls) || !cls.Equals("XYZ"))
        continue;

    HtmlElementCollection childDivs = element.Children.GetElementsByName("ABC");
    foreach (HtmlElement childElement in childDivs)
    {
        //grab links and other stuff same way
    }
}

另外,请注意用类名,而不是类,它可以把你的正确类的名称。只用下课将返回一个空字符串。这在 MSDN - 的setAttribute ,但不是在的getAttribute 。因此,它会导致混乱的一点点。

Also note the use of "className" instead of "class", it will get you the name of the proper class. Using just "class" will return an empty string. This is documented in MSDN - SetAttribute, but not in GetAttribute. So it causes a little bit of confusion.

这篇关于查找特定的HTML数据和HtmlElement元素(集合)和网页浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆