开发者

Write query to parse HTML DOCUMENT with HtmlAgilityPack

开发者 https://www.devze.com 2023-03-11 14:57 出处:网络
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\"

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']"开发者_如何学Python)
         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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号