我正在尝试从该站点获取第一张图片,但我不断收到错误消息,无法完全弄清楚为什么..
这是代码:
string pictureurl = webdoc.DocumentNode.SelectSingleNode("//div[2]/table[1]/tr/td/a").Attributes["href"].Value;
错误是Object reference not set to an instance of an object.
谁能告诉我为什么?如果我删除上面的行它工作正常..
我正在尝试从该站点获取第一张图片,但我不断收到错误消息,无法完全弄清楚为什么..
这是代码:
string pictureurl = webdoc.DocumentNode.SelectSingleNode("//div[2]/table[1]/tr/td/a").Attributes["href"].Value;
错误是Object reference not set to an instance of an object.
谁能告诉我为什么?如果我删除上面的行它工作正常..
使用//div[2]/table[1]/tbody/tr/td/a
. 根据配置设置 HtmlAgilityPack 插入一个 tbody 元素。
您的 xpath 不正确,无法获取 img
请使用这种方式
string pictureurl =
webdoc.DocumentNode
.SelectSingleNode("//div[2]/table[1]/tr/td/a/img")
.Attributes["src"].Value;
通过调试器仔细检查以下内容实际上返回了一些东西......
webdoc.DocumentNode.SelectSingleNode("//div[2]/table[1]/tr/td/a");
我的猜测不是,而是使用以下(注意额外的tbody
)..
webdoc.DocumentNode.SelectSingleNode("//div[2]/table[1]/tbody/tr/td/a");