几天来,我正在研究基于WebBrowser的webscraper。在使用Threads和DocumentCompleted事件的几个原型之后,我决定尝试看看我能否做出一个简单易懂的Webscraper。WebBrowser控件抛出看似随机NullReferenceException
目标是创建一个不涉及实际线程对象的WebScraper。我希望它能够以连续的步骤工作(即转到网址,执行操作,转到其他网址等)。
这是我走到这一步:
public static class Webscraper
{
private static WebBrowser _wb;
public static string URL;
//WebBrowser objects have to run in Single Thread Appartment for some reason.
[STAThread]
public static void Init_Browser()
{
_wb = new WebBrowser();
}
public static void Navigate_And_Wait(string url)
{
//Navigate to a specific url.
_wb.Navigate(url);
//Wait till the url is loaded.
while (_wb.IsBusy) ;
//Loop until current url == target url. (In case a website loads urls in steps)
while (!_wb.Url.ToString().Contains(url))
{
//Wait till next url is loaded
while (_wb.IsBusy) ;
}
//Place URL
URL = _wb.Url.ToString();
}
}
我是新手程序员,但我认为这是非常简单的代码。 这就是为什么我讨厌,由于某种原因该计划在这段代码引发的NullReferenceException的事实:
_wb.Url.ToString().Contains(url)
我只是叫_wb.Navigate()方法,因此NullReference不能在_wb对象本身。所以我能想象的唯一事情就是_wb.Url对象为null。但_wb.IsBusy()循环应该阻止这一点。
那么发生了什么,我该如何解决?
在我用的那一刻.Net 4.0,但这看起来像是我需要的东西。 – Jordy 2013-04-25 07:09:05