Xpath使用html敏捷包返回null

时间:2012-07-19 09:57:36

标签: c# html parsing xpath html-agility-pack

我正在使用Html Agility Pack获取页面上每个产品的信息:

我的代码是这样但节点返回null。我使用的是使用Google Chrome找到的Xpath。

private void getDataBtn_Click(object sender, EventArgs e)
    {
        if (URL != null)
        {
            HttpWebRequest request;
            HttpWebResponse response;
            StreamReader sr;

            List<string> Items = new List<string>(50);
            HtmlAgilityPack.HtmlDocument Doc = new HtmlAgilityPack.HtmlDocument();

            request = (HttpWebRequest)WebRequest.Create(URL);
            response = (HttpWebResponse)request.GetResponse();
            sr = new StreamReader(response.GetResponseStream());

            Doc.Load(sr);

            var Name = Doc.DocumentNode.SelectSingleNode("/html/body/table[2]/tbody/tr/td[2]/table/tbody/tr[2]/td/table/tbody/tr[2]/td[2]/table/tbody/tr[1]/td[3]/a");
        }
    }

我做错了什么?是否有任何其他工具可以创建与敏捷包兼容的xpath表达式?

1 个答案:

答案 0 :(得分:0)

因为此页面中没有此类节点。当你通过敏捷包(而不是浏览器)下载它时,该页面有这样的文字:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>HobbyKing Page not found.</title>
</head>

<body>
<img src="http://www.hobbyking.com/hk_logo.gif"><br>
<span style="font-family:Verdana, Arial, Helvetica, sans-serif">
<strong>Page no longer available</strong><br>
It seems you have landed on a page that doesnt exist anymore.<br>
Please update your links to point towards the correct hobbyking.com location;<br>
<a href="http://www.hobbyking.com/hobbyking/store/uh_index.asp">http://www.hobbyking.com/hobbyking/store/uh_index.asp</a><br>
<br>
If you continue to see this message, please email <a href="mailto:support@hobbyking.zendesk.com">support@hobbyking.zendesk.com</a></span>
</body>
</html>

您可以在页面中看到以下句子:
“请更新您的链接以指向正确的hobbyking.com位置;
http://www.hobbyking.com/hobbyking/store/uh_index.asp

P.S。你可以通过在visual-studio的调试中查看它来看到它。