基于查询的 HtmlAgilityPack 过滤 HTML
HtmlAgilityPack filtering HTML based on a query
我有一个包含两个 HTML 元素的块,如下所示:
<div class="a-row">
<a class="a-size-small a-link-normal a-text-normal" href="/Chemical-Guys-CWS-107-Extreme-Synthetic/dp/B003U4P3U0/ref=sr_1_1_sns?s=automotive&ie=UTF8&qid=1504525216&sr=1-1">
<span aria-label=".51" class="a-color-base sx-zero-spacing">
<span class="sx-price sx-price-large">
<sup class="sx-price-currency">$</sup>
<span class="sx-price-whole">19</span>
<sup class="sx-price-fractional">51</sup>
</span>
</span>
<span class="a-letter-space"></span>Subscribe & Save
</a>
</div>
HTML 的下一个块:
<div class="a-row a-spacing-none">
<a class="a-link-normal a-text-normal" href="https://rads.whosebug.com/amzn/click/com/B003U4P3U0" rel="nofollow noreferrer">
<span aria-label=".95" class="a-color-base sx-zero-spacing">
<span class="sx-price sx-price-large">
<sup class="sx-price-currency">$</sup>
<span class="sx-price-whole">22</span>
<sup class="sx-price-fractional">95</sup>
</span>
</span>
</a>
<span class="a-letter-space"></span>
<i class="a-icon a-icon-prime a-icon-small s-align-text-bottom" aria-label="Prime">
<span class="a-icon-alt">Prime</span>
</i>
</div>
这两个元素的结构非常相似,但诀窍是我想提取元素的值,它旁边包含一个带有 class 的 span 元素:aria-label="Prime"
这是我目前提取价格的方式,但效果不佳:
if (htmlDoc.DocumentNode.SelectNodes("//span[@class='a-color-base sx-zero-spacing']") != null)
{
var span = htmlDoc.DocumentNode.SelectSingleNode("//span[@class='a-color-base sx-zero-spacing']");
price = span.Attributes["aria-label"].Value;
}
这基本上是 selects HTML 位置 0 的元素,因为有不止一个元素。但这里的诀窍是我想 select 包含素数的 span 元素,就像我展示的 HTML 的第二部分一样......
如果不存在具有此类值的第二个元素,我将只使用我在那里写的第一种方法...
有人可以帮我解决这个问题吗? =)
我也试过这样的方法:
var pr = htmlDoc.DocumentNode.SelectNodes("//a[@class='a-link-normal a-text-normal']")
.Where(x => x.SelectSingleNode("//i[@class='a-icon a-icon-prime a-icon-small s-align-text-bottom']") != null)
.Select(x => x.SelectSingleNode("//span[@class='a-color-base sx-zero-spacing']").Attributes["aria-label"].Value);
但它仍然返回第一个元素 xD
新版本小伙伴们:
var pr = htmlDoc.DocumentNode.SelectNodes("//a[@class='a-link-normal a-text-normal']");
string prrrrrr = "";
for (int i = 0; i < pr.Count; i++)
{
if (pr.ElementAt(i).SelectNodes("//i[@class='a-icon a-icon-prime a-icon-small s-align-text-bottom']").ElementAt(i) != null)
{
prrrrrr = pr.ElementAt(i).SelectNodes("//span[@class='a-color-base sx-zero-spacing']").ElementAt(i).Attributes["aria-label"].Value;
}
}
所以我的想法是,我从 HTML 文件中取出所有 "a" 元素并创建一个 HTML 节点集合,然后循环遍历它们并查看是哪一个确实包含我正在寻找的元素然后匹配它...?
这里的问题是这个 if 语句总是通过:
if (pr.ElementAt(i).SelectNodes("//i[@class='a-icon a-icon-prime a-icon-small s-align-text-bottom']").ElementAt(i) != null)
如何遍历节点集合中的每个单独元素?
我认为你应该开始看 div
和 class a-row
的水平。然后循环并检查 div
是否包含 i
且 class area-label
等于 'Prime'。最后得到 span
与 a-color-base sx-zero-spacing
class 和属性值 aria-label
像这样:
HtmlNodeCollection nodes = htmlDoc.DocumentNode.SelectNodes("//div[starts-with(@class,'a-row')]");
foreach (HtmlNode node in nodes)
{
HtmlNode i = node.SelectSingleNode("i[@aria-label='Prime']");
if (i != null)
{
HtmlNode span = node.SelectSingleNode(".//span[@class='a-color-base sx-zero-spacing']");
if (span != null)
{
string currentValue = span.Attributes["aria-label"].Value;
}
}
}
我有一个包含两个 HTML 元素的块,如下所示:
<div class="a-row">
<a class="a-size-small a-link-normal a-text-normal" href="/Chemical-Guys-CWS-107-Extreme-Synthetic/dp/B003U4P3U0/ref=sr_1_1_sns?s=automotive&ie=UTF8&qid=1504525216&sr=1-1">
<span aria-label=".51" class="a-color-base sx-zero-spacing">
<span class="sx-price sx-price-large">
<sup class="sx-price-currency">$</sup>
<span class="sx-price-whole">19</span>
<sup class="sx-price-fractional">51</sup>
</span>
</span>
<span class="a-letter-space"></span>Subscribe & Save
</a>
</div>
HTML 的下一个块:
<div class="a-row a-spacing-none">
<a class="a-link-normal a-text-normal" href="https://rads.whosebug.com/amzn/click/com/B003U4P3U0" rel="nofollow noreferrer">
<span aria-label=".95" class="a-color-base sx-zero-spacing">
<span class="sx-price sx-price-large">
<sup class="sx-price-currency">$</sup>
<span class="sx-price-whole">22</span>
<sup class="sx-price-fractional">95</sup>
</span>
</span>
</a>
<span class="a-letter-space"></span>
<i class="a-icon a-icon-prime a-icon-small s-align-text-bottom" aria-label="Prime">
<span class="a-icon-alt">Prime</span>
</i>
</div>
这两个元素的结构非常相似,但诀窍是我想提取元素的值,它旁边包含一个带有 class 的 span 元素:aria-label="Prime"
这是我目前提取价格的方式,但效果不佳:
if (htmlDoc.DocumentNode.SelectNodes("//span[@class='a-color-base sx-zero-spacing']") != null)
{
var span = htmlDoc.DocumentNode.SelectSingleNode("//span[@class='a-color-base sx-zero-spacing']");
price = span.Attributes["aria-label"].Value;
}
这基本上是 selects HTML 位置 0 的元素,因为有不止一个元素。但这里的诀窍是我想 select 包含素数的 span 元素,就像我展示的 HTML 的第二部分一样...... 如果不存在具有此类值的第二个元素,我将只使用我在那里写的第一种方法...
有人可以帮我解决这个问题吗? =)
我也试过这样的方法:
var pr = htmlDoc.DocumentNode.SelectNodes("//a[@class='a-link-normal a-text-normal']")
.Where(x => x.SelectSingleNode("//i[@class='a-icon a-icon-prime a-icon-small s-align-text-bottom']") != null)
.Select(x => x.SelectSingleNode("//span[@class='a-color-base sx-zero-spacing']").Attributes["aria-label"].Value);
但它仍然返回第一个元素 xD
新版本小伙伴们:
var pr = htmlDoc.DocumentNode.SelectNodes("//a[@class='a-link-normal a-text-normal']");
string prrrrrr = "";
for (int i = 0; i < pr.Count; i++)
{
if (pr.ElementAt(i).SelectNodes("//i[@class='a-icon a-icon-prime a-icon-small s-align-text-bottom']").ElementAt(i) != null)
{
prrrrrr = pr.ElementAt(i).SelectNodes("//span[@class='a-color-base sx-zero-spacing']").ElementAt(i).Attributes["aria-label"].Value;
}
}
所以我的想法是,我从 HTML 文件中取出所有 "a" 元素并创建一个 HTML 节点集合,然后循环遍历它们并查看是哪一个确实包含我正在寻找的元素然后匹配它...?
这里的问题是这个 if 语句总是通过:
if (pr.ElementAt(i).SelectNodes("//i[@class='a-icon a-icon-prime a-icon-small s-align-text-bottom']").ElementAt(i) != null)
如何遍历节点集合中的每个单独元素?
我认为你应该开始看 div
和 class a-row
的水平。然后循环并检查 div
是否包含 i
且 class area-label
等于 'Prime'。最后得到 span
与 a-color-base sx-zero-spacing
class 和属性值 aria-label
像这样:
HtmlNodeCollection nodes = htmlDoc.DocumentNode.SelectNodes("//div[starts-with(@class,'a-row')]");
foreach (HtmlNode node in nodes)
{
HtmlNode i = node.SelectSingleNode("i[@aria-label='Prime']");
if (i != null)
{
HtmlNode span = node.SelectSingleNode(".//span[@class='a-color-base sx-zero-spacing']");
if (span != null)
{
string currentValue = span.Attributes["aria-label"].Value;
}
}
}