C# 使用 HtmlElement(Collection) 和 webbrowser 在 html 中查找特定数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1157258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find specific data in html with HtmlElement(Collection) and webbrowser
提问by
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 的 div,然后在其中我想遍历一堆名为 ABC 的元素。然后获取里面的链接(a href)和其他可能的信息。
How do I find the div with XYZ from webBrowser1.Document.Links
and any subitems I want?
如何找到带有 XYZ 的 divwebBrowser1.Document.Links
以及我想要的任何子项?
采纳答案by Stan R.
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.
首先你说你想找到一个类名为 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
}
}
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.
还要注意使用“className”而不是“class”,它会给你正确的类的名称。仅使用“class”将返回一个空字符串。这记录在MSDN-SetAttribute 中,但未记录在GetAttribute 中。所以它会引起一些混乱。