HTML Agility Pack-如何选择正确的跨度类别 [英] Html Agility Pack - how to select correct span class
问题描述
我正试图在亚马逊页面上找到最低的价格.让我们以这个网址为例:
I'm trying to find lowest price on Amazon pages. Let's use this url as an example:
http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=9963BB#/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=E999-4701&rh=i%3Aaps%2Ck%3AE999-4701
我想找到最低的价格...新货来源"右边的数字.
I want to find the lowest price ... the number to the right of "new from".
这是我尝试过的:
using (TextWriter tw = new StreamWriter(@"D:\AmazonUrls.txt"))
{
foreach (string item in list)
{
var webGet = new HtmlWeb();
var document = webGet.Load(item);
var lowestPrice = document.DocumentNode.SelectSingleNode("//span[@id='subPrice']");
if (lowestPrice != null)
{
Console.WriteLine(lowestPrice);
}
}
}
我没有任何结果.我要去哪里错了?
I'm not getting any result. Where am I going wrong?
推荐答案
您要的是id
subPrice的节点,但实际上class
具有subPrice:
You are asking for nodes with an id
of subPrice, but it is in fact class
that has subPrice:
<span class="subPrice">
<a href="http://www.amazon.com/gp/offer-listing/B001BA0W06/ref=sr_1_6_olp?ie=UTF8&qid=1334090832&sr=8-6&condition=new">5 new</a>
from <span class="price">$245.90</span></span>
所以
var lowestPrice = document.DocumentNode.SelectSingleNode("//span[@class='subPrice']");
应该为您带来想要的东西.但是,您提供的示例页面有几个与该模式匹配的节点,因此您遇到的问题是要选择多个节点,然后遍历它们以确定哪个特权最低.
should get you what you want. However, the example page that you give has several nodes that match that pattern, so you problem want to select multiple nodes and then loop through them to decide which has the lowest privce.
这篇关于HTML Agility Pack-如何选择正确的跨度类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!