Web浏览器导航未发生

时间:2014-03-12 19:10:20

标签: c# asp.net c#-4.0

我正在尝试导航到存储在TrimedPages中的不同网址链接,但每次我尝试将其导航到TrimedPages [0]索引时,它都不会导航到新网址,而只是导航到旧网址:/

private void webBrowser1_DocumentCompleted_1(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    do
    {
        string url = webBrowser1.Url.ToString();
        HtmlDocument doc = webBrowser1.Document;

        HtmlElementCollection collection = webBrowser1.Document.GetElementsByTagName("h2");

        HtmlElementCollection collection2 = webBrowser1.Document.GetElementsByTagName("li");

        HtmlElementCollection col = doc.GetElementsByTagName("p");

        HtmlElementCollection collect = webBrowser1.Document.GetElementsByTagName("div");

        if (IsRanBefore == false)
        {
            foreach (HtmlElement item in collection2)
            {
                att = item.GetAttribute("className");
                if (att == "nav-current")
                {
                    MainPageLink = item.InnerHtml;
                    string[] Trim = MainPageLink.Split(new[] { '-', '\"' }, StringSplitOptions.RemoveEmptyEntries);
                    MainPageLink = Trim[2] + "/";
                }
            }

            foreach (HtmlElement item in collect)
            {
                string cls = item.GetAttribute("className");
                if (cls == "nav")
                {
                    Pages.Add(item.InnerHtml);
                    break;
                }
            }
            foreach (string item in Pages)
            {
                string[] Trim = item.Split(TrimedWords, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 8; i < Trim.Length; i += 8)
                {
                    TrimedPages.Add("http://www.bbc.co.uk" + MainPageLink + Trim[i]);
                }
            }

            IsRanBefore = true;
        }

        foreach (HtmlElement item in collection)
        {
            string cls = item.GetAttribute("className");
            if (cls == "link title")
            {
                string inner = item.InnerText;
                links.Add(item.InnerHtml);
            }
        }

        System.IO.File.WriteAllLines("NewLinksss.txt", links.ToArray());

        foreach (string item in links)
        {
            string[] Trim = item.Split(TrimedWords, StringSplitOptions.RemoveEmptyEntries);
            Trim[1] = "http://www.bbc.co.uk" + Trim[1];
            if (!trimedlinks.Contains(Trim[1]))
            {
                trimedlinks.Add(Trim[1]);
            }
        }
        links = new List<string>();
        MessageBox.Show("Crawling Done of web page" + TrimedPages[0]);
        if (TrimedPages.Count > 0)
        {
            MessageBox.Show("Page removed " + TrimedPages[0]);
            TrimedPages.RemoveAt(0);


            System.IO.File.WriteAllLines("NewLinks.txt", trimedlinks.ToArray());

            uri = TrimedPages[0];
            webBrowser1.Navigate(uri);
            Uri myUri = new Uri(uri);
            webBrowser1.Url = myUri;
            MessageBox.Show(webBrowser1.Url.ToString());
            MessageBox.Show(uri);
        }

    } while (TrimedPages.Count>0);
}

0 个答案:

没有答案