Html Agility Pack - how to select correct span class

偶尔善良 提交于 2020-01-04 02:34:09

问题


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".

Here's what I have tried:

        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?


回答1:


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>

so,

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.



来源:https://stackoverflow.com/questions/10096181/html-agility-pack-how-to-select-correct-span-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!