C# WebBrowser 在 for 循环中使用时卡在导航上
C# WebBrowser stuck on navigating when used in for loop
我有一个 for 循环可以改变 URL
for (int i = 1; i < max; i += 50)
{
completed = false;
string currkey = country;
crawler.Navigate(new Uri("http://www.example.net/func.php?dom=" + currkey + "&key=&start=" + i));
Console.WriteLine("Navigating to " + "http://www.example.net/func.php?dom=" + currkey + "&key=&start=" + i);
while (!completed)
{
Application.DoEvents();
Thread.Sleep(500);
}
}
这是我的文档完成处理程序
crawler.Refresh();
Console.WriteLine("Getting universities");
getUniversities();
Console.WriteLine("Finished getting universities");
completed = true;
当我摆脱 for 循环并使用单个 link 时,它似乎可以正确导航到网站,但是当我使用 for 循环按顺序加载网站时,网络浏览器似乎卡在第二次迭代中。
示例:
currkey = 美国
在第一次迭代中,网站 link 将是 http://www.example.net/func.php?dom="United States"&key=&start=1, and on the next one it will be http://www.example.net/func.php?dom="United States"&key=&start=51。尝试加载第二个 link.
时导航卡住了
我已经用boolean completed表示当前迭代已经完成,但是还是卡住了。
感谢任何形式的帮助
您的 Thread.Sleep
调用正在阻止 WebBrowser
继续加载。您应该做的是附加到 DocumentCompleted
事件,然后加载下一页。请不要在 WinForms 中使用此 while/sleep 组合 - 您应该使用控件公开的事件。
附加事件:
crawler.DownloadCompleted += CrawlerDocumentCompleted;
事件处理程序:
private void CrawlerDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//The document has loaded - now do something
}
最后的想法
看起来你在实现一个爬虫,为什么你使用 WinForms 中的 WebBrowser
控件来导航。您肯定只对服务器提供的 html 感兴趣吗?或者页面是否使用 JavaScript 将其他元素加载到 DOM,要求您使用 WebBrowser
?
您可以使用 WebClient
class 和 DownloadString
或 DownloadStringAsync
方法。参见 https://msdn.microsoft.com/en-us/library/fhd1f0sw(v=vs.110).aspx
我有一个 for 循环可以改变 URL
for (int i = 1; i < max; i += 50)
{
completed = false;
string currkey = country;
crawler.Navigate(new Uri("http://www.example.net/func.php?dom=" + currkey + "&key=&start=" + i));
Console.WriteLine("Navigating to " + "http://www.example.net/func.php?dom=" + currkey + "&key=&start=" + i);
while (!completed)
{
Application.DoEvents();
Thread.Sleep(500);
}
}
这是我的文档完成处理程序
crawler.Refresh();
Console.WriteLine("Getting universities");
getUniversities();
Console.WriteLine("Finished getting universities");
completed = true;
当我摆脱 for 循环并使用单个 link 时,它似乎可以正确导航到网站,但是当我使用 for 循环按顺序加载网站时,网络浏览器似乎卡在第二次迭代中。
示例: currkey = 美国
在第一次迭代中,网站 link 将是 http://www.example.net/func.php?dom="United States"&key=&start=1, and on the next one it will be http://www.example.net/func.php?dom="United States"&key=&start=51。尝试加载第二个 link.
时导航卡住了我已经用boolean completed表示当前迭代已经完成,但是还是卡住了。
感谢任何形式的帮助
您的 Thread.Sleep
调用正在阻止 WebBrowser
继续加载。您应该做的是附加到 DocumentCompleted
事件,然后加载下一页。请不要在 WinForms 中使用此 while/sleep 组合 - 您应该使用控件公开的事件。
附加事件:
crawler.DownloadCompleted += CrawlerDocumentCompleted;
事件处理程序:
private void CrawlerDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//The document has loaded - now do something
}
最后的想法
看起来你在实现一个爬虫,为什么你使用 WinForms 中的 WebBrowser
控件来导航。您肯定只对服务器提供的 html 感兴趣吗?或者页面是否使用 JavaScript 将其他元素加载到 DOM,要求您使用 WebBrowser
?
您可以使用 WebClient
class 和 DownloadString
或 DownloadStringAsync
方法。参见 https://msdn.microsoft.com/en-us/library/fhd1f0sw(v=vs.110).aspx