【发布时间】:2012-05-16 10:50:25
【问题描述】:
public partial class Form1 : Form
{
int y = 0;
string url = @"http://www.google.co.il";
string urls = @"http://www.bing.com/images/search?q=cat&go=&form=QB&qs=n";
public Form1()
{
InitializeComponent();
//webCrawler(urls, 3);
List<string> a = webCrawler(urls, 1);
//GetAllImages();
}
private int factorial(int n)
{
if (n == 0) return 1;
else y = n * factorial(n - 1);
listBox1.Items.Add(y);
return y;
}
private List<string> getLinks(HtmlAgilityPack.HtmlDocument document)
{
List<string> mainLinks = new List<string>();
if (document.DocumentNode.SelectNodes("//a[@href]") == null)
{ }
foreach (HtmlNode link in document.DocumentNode.SelectNodes("//a[@href]"))
{
var href = link.Attributes["href"].Value;
mainLinks.Add(href);
}
return mainLinks;
}
private List<string> webCrawler(string url, int levels)
{
HtmlAgilityPack.HtmlDocument doc;
HtmlWeb hw = new HtmlWeb();
List<string> webSites;// = new List<string>();
List<string> csFiles = new List<string>();
csFiles.Add("temp string to know that something is happening in level = " + levels.ToString());
csFiles.Add("current site name in this level is : "+url);
/* later should be replaced with real cs files .. cs files links..*/
doc = hw.Load(url);
webSites = getLinks(doc);
if (levels == 0)
{
return csFiles;
}
else
{
int actual_sites = 0;
for (int i = 0; i < webSites.Count() && i< 100000; i++) // limiting ourseleves for 20 sites for each level for now..
//or it will take forever.
{
string t = webSites[i];
/*
if (!webSites.Contains(t))
{
webCrawler(t, levels - 1);
}
*/
if ( (t.StartsWith("http://")==true) || (t.StartsWith("https://")==true) ) // replace this with future FilterJunkLinks function
{
actual_sites++;
csFiles.AddRange(webCrawler(t, levels - 1));
richTextBox1.Text += t + Environment.NewLine;
}
}
// report to a message box only at high levels..
if (levels==1)
MessageBox.Show(actual_sites.ToString());
return csFiles;
}
}
在几个站点被发送到getLinks函数后抛出异常。
例外是在getLinks函数就行了:
foreach (HtmlNode link in document.DocumentNode.SelectNodes("//a[@href]"))
对象引用未设置为对象的实例
我尝试在那里使用 IF 来检查它是否为空,然后我做了return mainLinks; 这是一个列表。
但如果我这样做,我不会从网站上获得所有链接。
如果我使用 url (www.google.co.il) 现在我在构造函数中使用 url,几秒钟后我会得到同样的异常。
我不知道为什么会抛出这个异常。这个例外有什么原因吗?
System.NullReferenceException 未处理
Message=对象引用未设置为对象的实例。
来源=GatherLinks
堆栈跟踪:
在 D:\C-Sharp\GatherLinks\GatherLinks\GatherLinks\Form1.cs: 55 行中的 GatherLinks.Form1.getLinks(HtmlDocument 文档)
在 D:\C-Sharp\GatherLinks\GatherLinks\GatherLinks\Form1.cs: 76 行中的 GatherLinks.Form1.webCrawler(字符串 url,Int32 级别)
在 D:\C-Sharp\GatherLinks\GatherLinks\GatherLinks\Form1.cs:line 104 中的 GatherLinks.Form1.webCrawler(字符串 url,Int32 级别)
在 D:\C-Sharp\GatherLinks\GatherLinks\GatherLinks\Form1.cs:line 29 中的 GatherLinks.Form1..ctor()
在 D:\C-Sharp\GatherLinks\GatherLinks\GatherLinks\Program.cs:line 18 中的 GatherLinks.Program.Main()
在 System.AppDomain._nExecuteAssembly(Assembly 程序集,String[] args)
在 System.AppDomain.ExecuteAssembly(字符串 assemblyFile,证据 assemblySecurity,String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback 回调, 对象状态)
在 System.Threading.ThreadHelper.ThreadStart()
【问题讨论】:
-
如果您能突出显示
getLinks中的哪一行是第 55 行,将会有所帮助。 -
您需要弄清楚为什么您的对象引用为空,并确保在对所述对象执行任何操作之前验证它不是
null。在我们为您提供帮助之前,您还有很多工作要做。 -
也许您发现了一个没有任何链接的页面?
-
旁注:如果你真的在你的代码中抓取了 url/urls,我很确定你违反了他们的使用条款。
-
菲尔伯特我不知道。我这样做只是为了我自己的测试。感谢您提供的信息,我将使用自己的网站或公共网站。
标签: c# html-parsing html-agility-pack