1

I want to get the A href of that element in span class="floatClear" whose rating is minimum in
span class="star-img stars_4"

How can I use HtmlAgilityPack to achieve this behaviour I have give the html source of my file

<div class="businessresult">  //will repeat


      <div class="rightcol">

       <div class="rating">

        <span class="star-img stars_4">
          <img height="325" width="84" src="http://media1.px" alt="4.0 star rating"   **title**="4.0 star rating">
         </span>

        </div>
      </div>

        <span class="floatClear">
             <a class="ybtn btn-y-s" href="/writeareview/biz/KaBw8UEm8u6war_loc%NY">
        </span>
</div>

The query I have written

var lowestreview = 
      from main in htmlDoc.DocumentNode.SelectNodes("//div[@class='rightcol']") 
       from rating in htmlDoc.DocumentNode.SelectNodes("//div[@class='rating']")
         from ratingspan in htmlDoc.DocumentNode.SelectNodes("//span[@class='star-img stars_4']")
          from floatClear in htmlDoc.DocumentNode.SelectNodes("//span[@class='floatClear']")
       select new { Rate = ratingspan.InnerText, AHref = floatClear.InnerHtml };

But I do not know how to apply condition here at last line of LINQ query!

4
  • Are you sure this is the right query? You are selecting a bunch of independant nodes: You get all "rating" divs whether or not they are withing a "rightcol" div (and so on). Commented Jun 8, 2011 at 7:21
  • 1
    I am not sure that,s why i am asking! Commented Jun 8, 2011 at 7:25
  • Does it has to be in one query? Commented Jun 10, 2011 at 17:22
  • not necessary i just need to parse the html and get url of the lowest reviewed product. Commented Jun 10, 2011 at 17:31

1 Answer 1

2

Don't select "rating" from the entire htmlDoc, select it from the previously found "main".

I guess you need something like:

var lowestreview = 
  from main in htmlDoc.DocumentNode.SelectNodes("//div[@class='rightcol']") 
   from rating in main.SelectNodes("//div[@class='rating']")
     from ratingspan in rating.SelectNodes("//span[@class='star-img stars_4']")
      from floatClear in ratingspan.SelectNodes("//span[@class='floatClear']")
   select new { Rate = ratingspan.InnerText, AHref = floatClear.InnerHtml };

I hope it will not crash if some of those divs ans spans are not present: a previous version of the HtmlAgilityPack returned null instead of an empty list when the SelectNodes didn't find anything.

EDIT
You probably also need to change the "xpath query" for the inner selects: change the "//" into ".//" (extra . at the beginning) to signal that you really want a subnode. If the AgilityPack works the same as regular XML-XPath (I'm not 100% sure) then a "//" at the beginning will search from the root of the document, even if you specify it from a subnode. A ".//" will always search from the node you are searching from.

A main.SelectNodes("//div[@class='rating']") will (probably) also find <div class="rating">s outside the <div class="rightcol"> you found in the previous line. A main.SelectNodes(".//div[@class='rating']") should fix that.

Sign up to request clarification or add additional context in comments.

1 Comment

I think 2nd last line should be from floatClear in htmlDoc.DocumentNode.SelectNodes("//span[@class='floatClear']") //as floatclear is not sub node of rightcol div.Also in EDIT you mention to replace the // with what is it (.//)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.