Skip to content Skip to sidebar Skip to footer

Write Query To Parse HTML DOCUMENT With HtmlAgilityPack

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 behaviou

Solution 1:

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.


Post a Comment for "Write Query To Parse HTML DOCUMENT With HtmlAgilityPack"