xpath表达式从链接中选择href值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xpath表达式从链接中选择href值相关的知识,希望对你有一定的参考价值。
我有这样的html代码
<a class="cat" href="/Home/txtdata0/">txtdata0</a>
<a class="cat" href="/Home/txtdata1/">txtdata1</a>
<a class="cat" href="/Home/txtdata2/">txtdata2</a>
<a class="cat" href="/Home/txtdata3/">txtdata3</a>
访问链接的所有文本我使用这样的XPATH(如在C#中的Visual Studio中)
.//a[@class="cat"]
访问链接的所有href值我使用这样的XPATH(如在C#中的Visual Studio中)
.//a[@class="cat"]/@href
谷歌Chrome Xpath助手秀(.//a[@class="cat“]和.//a[@class="cat"]/@href)两个结果都是正确的
txtdata0
txtdata1
txtdata2
txtdata3
和
/Home/txtdata0/
/Home/txtdata1/
/Home/txtdata2/
/Home/txtdata3/
带有这样的Xpath的Visual Studio //一个[@class = “cat ”]显示:
txtdata0
txtdata1
txtdata2
txtdata3
和这样的Xpath .//a[@class="cat"]/@href显示:
txtdata0
txtdata1
txtdata2
txtdata3
为什么第二输出与第一输出相同?
程序代码
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(seturl);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = null;
if (response.CharacterSet == null)
{
readStream = new StreamReader(receiveStream);
}
else
{
readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
}
data = readStream.ReadToEnd();
response.Close();
readStream.Close();
}
doc.LoadHtml(data);
HtmlAgilityPack.HtmlNodeCollection bodynode = doc.DocumentNode.SelectNodes(".//a[@class="cat"]");
HtmlAgilityPack.HtmlNodeCollection bodynod = doc.DocumentNode.SelectNodes(".//a[@class="cat"]/@href");
MessageBox.Show(bodynode.Count.ToString());
MessageBox.Show(bodynod.Count.ToString());
for (int i = 0; i < bodynode.Count; i++)
{
MessageBox.Show(bodynode[i].InnerText.ToString() + " - " + bodynod[i].InnerText.ToString());
}
答案
如果我没记错的话可以像这样提取HAP属性
string _tmpUrl = documentUrl.DocumentNode.SelectNodes("//a[@class='cat']")[i].Attributes["href"].Value;
以上是关于xpath表达式从链接中选择href值的主要内容,如果未能解决你的问题,请参考以下文章
Python爬虫从入门到放弃(十四)之 Scrapy框架中选择器的用法