如何使用htmlagilitypack查找锚点的下一个元素

时间:2019-03-14 00:00:39

标签: c# html-agility-pack

这是我的html元素:

<a href="#" class="A">Sana</a>
<b><span class="B">1000</span>Dollar</b>

我想与htmlagilitypack一起阅读1000(价格)。
Sana innerText具有唯一性,但DollarinnerText不是唯一。
目标页面上还有许多类,名称分别为AB
我想读取具有element a innerText的锚点(Sana)并找到该锚点(element b)的下一个元素并读取该锚点的innerText。
这是我的代码:

HttpWebRequest req = (HttpWebRequest) WebRequest.Create("goal web site");
req.Method = "GET";
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*;q=0.8";
req.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36";
req.ContentType = "text/html; charset=utf-8";
req.Referer = "";
req.KeepAlive = true;
req.Timeout = 25000;
req.AllowAutoRedirect = true;

CookieContainer cookieJar1 = new CookieContainer();
req.CookieContainer = cookieJar1;

HttpWebResponse res = (HttpWebResponse) req.GetResponse();

foreach(Cookie cookie in res.Cookies) {
    cookieJar1.Add(new Cookie(cookie.Name.Trim(), cookie.Value.Trim(), "/", cookie.Domain));
}

Stream Stream = res.GetResponseStream();
StreamReader reader = new StreamReader(Stream);
string reader_str = reader.ReadToEnd();

HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.LoadHtml(reader_str);

在这里我该如何阅读1000价格?

reader.Close();
Stream.Close();
res.Close();

1 个答案:

答案 0 :(得分:1)

您可以尝试使用以下XPath * 表达式:

var xpath = "//a[.='Sana']/following-sibling::b[1]/span";
string price = htmlDoc.DocumentNode
                      .SelectSingleNode(xpath)
                      .InnerText;
Console.WriteLine(price.Text);

demo

XPath表达式损坏:

  • //a[.='Sana'] :找到<a>个元素,其内部文本等于文档中任何位置的“ Sana”
  • /following-sibling::b[1] :找到在上一步中找到的<b>个元素之后的第一个同级元素<a>
  • /span :找到上一步中找到的<span>元素的子元素<b>

*)如果您不熟悉“ XPath”主题,请阅读。这是MSDN上的一个:https://msdn.microsoft.com/en-us/library/ms256086(v=vs.110).aspx