Reputation: 1147
I'm trying to get the first image from this site but I keep getting an error and can't quite figure out why..
Here's the code:
string pictureurl = webdoc.DocumentNode.SelectSingleNode("//div[2]/table[1]/tr/td/a").Attributes["href"].Value;
and the error is Object reference not set to an instance of an object.
Can anyone please tell me why? if I remove the above line it works fine..
Upvotes: 0
Views: 99
Reputation: 19953
Double check through the debugger that the following actually returns something...
webdoc.DocumentNode.SelectSingleNode("//div[2]/table[1]/tr/td/a");
My guess is not, and instead use the following (note the extra tbody
)..
webdoc.DocumentNode.SelectSingleNode("//div[2]/table[1]/tbody/tr/td/a");
Upvotes: 1
Reputation: 11191
Your xpath is not correct to get a img
please use this way
string pictureurl =
webdoc.DocumentNode
.SelectSingleNode("//div[2]/table[1]/tr/td/a/img")
.Attributes["src"].Value;
Upvotes: 1
Reputation: 2146
Use //div[2]/table[1]/tbody/tr/td/a
. Depending on the configuration settings HtmlAgilityPack inserts a tbody element.
Upvotes: 1