I want use this code but it returns NULL in all this parameters ! would you please help how to fix it or what is the problem ?
click here for a picture that shown what happened.
code :
HtmlWeb hw = new HtmlWeb();
private void button1_Click(object sender, EventArgs e)
{
Crawler("http://www.avval.ir/directory", 0);
}
public void Crawler(string url, int pid)
{
try
{
HtmlAgilityPack.HtmlDocument doc = hw.Load("http://www.avval.ir/directory");
HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//a[@class='metatag-topi metatag-keywords']");
HtmlNodeCollection nodes2 = doc.DocumentNode.SelectNodes("//div[@class='pagination']/a[@href]");
HtmlNodeCollection phone = doc.DocumentNode.SelectNodes("//p[@class='phone fRight ml10']");
}
}
Man, it seems you have a typo in your xpath selector, see:
HtmlNodeCollection nodes =
doc.DocumentNode.SelectNodes("//a[@class='metatag-topi metatag-keywords']");
Here you use //a[@class='metatag-topi metatag-keywords']
expression that selects all the <a>
tags with class
attribute value metatag-topi metatag-keywords
. But the thing is, the page you're trying to apply your expression doesn't contain such elements, but has some <a class="metatag-topic metatag-keywords">
(missing c
letter in the end of the word topic
).
Just change that line to:
HtmlNodeCollection nodes =
doc.DocumentNode.SelectNodes("//a[@class='metatag-topic metatag-keywords']");
and it should work.
The DocumentNode.SelectNodes
could return null if no nodes are not found.
You may check this related Why DocumentNode.SelectNodes returns null