简体   繁体   中英

How to get list htmldocument from list url?

I trying to use webbrowser to navigate list of url and get list htmldocument and I use this my code:

WebBrowser webBrowser1 = new WebBrowser();
    private void Form1_Load(object sender, EventArgs e)
    {
        string[] url = new string[] { @"http://x2.blogtruyen.com/2010/07/gto-shonan-14-days-chap-22.html", @"http://x2.blogtruyen.com/2010/07/gto-shonan-14-days-chap-23.html", @"http://x2.blogtruyen.com/2010/10/gto-shonan-14-days-chap-24.html" };
        foreach (string item in url)
        {
            webBrowser1.Navigate(new Uri(item));
            webBrowser1.ScriptErrorsSuppressed = true;
            webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
        }
    }
    void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        if (this.webBrowser1.ReadyState == WebBrowserReadyState.Complete)
        {
            string s = "";
            HtmlDocument doc = webBrowser1.Document;
            HtmlElementCollection images = doc.Images;
            foreach (HtmlElement item in images)
            {
                s += item.GetAttribute("src") + "\r\n";
            }
            MessageBox.Show(s);
            s = "";
        }
    }

But when I run my application, it's not work correctly!

My question: how to resolve this problem! Thank for advance!

Well, first "it's not work correctly" doesn't tell us anything about WHAT ISN'T WORKING.

Next, employing my psychic powers...

  1. Move the .ScriptErrorsSuppressed assignment outside your loop.
  2. Move the event hookup outside your loop.
  3. .Navigate is not a blocking call. You need to kick off the navigation on the first element, then do each subsequent navigation at the end your your event handler. OR use a sync object (like an AutoResetEvent) to make the .Navigate function pretend to be synchronous.

Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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