XPath,select 来自 HTML 中多个节点的多个元素
XPath, select multiple elements from multiple nodes in HTML
我就是想不通这个。
我必须搜索所有具有 classes 和 "item extend featured"
值的节点(代码如下)。在那些 classes 中,我需要 select <h2 class="itemtitle">
的每个 InnerText 和其中的 href
值,以及来自 <div class="title-additional">
的所有 InnerText。
<li class="item extend featured">
<div class="title-box">
<h2 class="itemtitle">
<a target="_top" href="www.example.com/example1/example2/exammple4/example4" title="PC Number 1">PC Number 1</a>
</h2>
<div class="title-additional">
<div class="title-km">150 km</div>
<div class="title-year">2009</div>
<div class="title-price">250 €</div>
</div>
输出应该是这样的:
Title:
href:
Title-km:
Title-year:
Title-Price:
--------------
Title:
href:
Title-km:
Title-year:
Title-Price:
--------------
那么,问题来了,如何从每个节点遍历我上面需要的html和select项中的所有"item extend featured"
个节点?
据我了解,像这样的东西应该可以工作,但中途中断了
编辑:我刚注意到,网站上有一些广告完全相同 class,但它们显然没有我需要的元素。更多问题需要思考。
var items1 = htmlDoc.DocumentNode.SelectNodes("//*[@class='item extend featured']");
foreach (var e in items1)
{
var test = e.SelectSingleNode(".//a[@target='_top']").InnerText;
Console.WriteLine(test);
}
您要实现的目标需要多个 XPath 表达式,因为您不能 return 使用一个查询在不同级别获得多个结果(除非您可能使用 Union)。
您可能正在寻找与此类似的内容:
var listItems = htmlDoc.DocumentNode.SelectNodes("//li[@class='item extend featured']");
foreach(var li in listItems) {
var title = li.SelectNodes("//h2/a/text()");
var href = li.SelectNodes("//h2/a/@href");
var title_km = li.SelectNodes("//div[@class='title-additional']/div[@class='title-km']/text()");
var title_... // other divs
}
注意:代码未经测试
var page = new HtmlDocument();
page.Load(path);
var lists = page.DocumentNode.SelectNodes("//li[@class='item extend featured']");
foreach(var list in lists)
{
var link = list.SelectSingleNode(".//*[@class='itemtitle']/a");
string title = link.GetAttributeValue("title", string.Empty);
string href = link.GetAttributeValue("href", string.Empty);
string km = list.SelectSingleNode(".//*[@class='title-km']").InnerText;
string year = list.SelectSingleNode(".//*[@class='title-year']").InnerText;
string price = list.SelectSingleNode(".//*[@class='title-price']").InnerText;
Console.WriteLine("Title: %s\r\n href: %s\r\n Title-km: %s\r\n Title-year: %s\r\n Title-Price: %s\r\n\r\n", title, href, km, year, price);
}
我就是想不通这个。
我必须搜索所有具有 classes 和 "item extend featured"
值的节点(代码如下)。在那些 classes 中,我需要 select <h2 class="itemtitle">
的每个 InnerText 和其中的 href
值,以及来自 <div class="title-additional">
的所有 InnerText。
<li class="item extend featured">
<div class="title-box">
<h2 class="itemtitle">
<a target="_top" href="www.example.com/example1/example2/exammple4/example4" title="PC Number 1">PC Number 1</a>
</h2>
<div class="title-additional">
<div class="title-km">150 km</div>
<div class="title-year">2009</div>
<div class="title-price">250 €</div>
</div>
输出应该是这样的:
Title:
href:
Title-km:
Title-year:
Title-Price:
--------------
Title:
href:
Title-km:
Title-year:
Title-Price:
--------------
那么,问题来了,如何从每个节点遍历我上面需要的html和select项中的所有"item extend featured"
个节点?
据我了解,像这样的东西应该可以工作,但中途中断了
编辑:我刚注意到,网站上有一些广告完全相同 class,但它们显然没有我需要的元素。更多问题需要思考。
var items1 = htmlDoc.DocumentNode.SelectNodes("//*[@class='item extend featured']");
foreach (var e in items1)
{
var test = e.SelectSingleNode(".//a[@target='_top']").InnerText;
Console.WriteLine(test);
}
您要实现的目标需要多个 XPath 表达式,因为您不能 return 使用一个查询在不同级别获得多个结果(除非您可能使用 Union)。
您可能正在寻找与此类似的内容:
var listItems = htmlDoc.DocumentNode.SelectNodes("//li[@class='item extend featured']");
foreach(var li in listItems) {
var title = li.SelectNodes("//h2/a/text()");
var href = li.SelectNodes("//h2/a/@href");
var title_km = li.SelectNodes("//div[@class='title-additional']/div[@class='title-km']/text()");
var title_... // other divs
}
注意:代码未经测试
var page = new HtmlDocument();
page.Load(path);
var lists = page.DocumentNode.SelectNodes("//li[@class='item extend featured']");
foreach(var list in lists)
{
var link = list.SelectSingleNode(".//*[@class='itemtitle']/a");
string title = link.GetAttributeValue("title", string.Empty);
string href = link.GetAttributeValue("href", string.Empty);
string km = list.SelectSingleNode(".//*[@class='title-km']").InnerText;
string year = list.SelectSingleNode(".//*[@class='title-year']").InnerText;
string price = list.SelectSingleNode(".//*[@class='title-price']").InnerText;
Console.WriteLine("Title: %s\r\n href: %s\r\n Title-km: %s\r\n Title-year: %s\r\n Title-Price: %s\r\n\r\n", title, href, km, year, price);
}