【问题标题】:Find specific data in html with HtmlElement(Collection) and webbrowser使用 HtmlElement(Collection) 和 webbrowser 在 html 中查找特定数据
【发布时间】:2010-11-12 12:53:44
【问题描述】:

我想找到一个类名为 XYZ 的 div,然后我想在其中循环遍历一组名为 ABC 的元素。然后抓取里面的链接(a href)和可能的其他信息。

如何从webBrowser1.Document.Links 中找到带有 XYZ 的 div 以及我想要的任何子项?

【问题讨论】:

    标签: c# html browser


    【解决方案1】:

    首先你说你想找到一个类名为 XYZ 的 div,那么你为什么要在 webBrowser1.Documnet.Links 中寻找呢?先找到 Div,然后再访问其中的链接。

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

    还要注意使用“className”而不是“class”,它会为您提供正确类的名称。仅使用“类”将返回一个空字符串。这记录在 MSDN - SetAttribute 中,但不在 GetAttribute 中。所以会引起一些混乱。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-11
      • 1970-01-01
      • 2011-01-30
      • 2015-01-31
      • 2013-10-29
      • 2013-06-20
      相关资源
      最近更新 更多