Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
378 views
in Technique[技术] by (71.8m points)

c# - Find specific data in html with HtmlElement(Collection) and webbrowser

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...