HTML agility pack get all divs with class

Burak Gazi picture Burak Gazi · Feb 10, 2014 · Viewed 18.5k times · Source

I am trying to scape a complicated HTMl. I need to get some text from div's with certain class.

What I am trying to do is have the html agility pack to go over the whole html and find all divs whos class contains "listevent" and return me those.

When I searched online I found out that If I map it , it is possible, but some of these divs are under somemany divs so trying to find some easy way.

The HTML looks like this

<div>
    <div>
       <table>
          <tr>
            <td>
              <div class="thisone listevent"></td>
            <td>
              <div class="thisone listevent"></td>
           </tr>
         </table>
     </div>
 </div>

Answer

Mike Koder picture Mike Koder · Feb 10, 2014

You could use SelectNodes method

foreach(HtmlNode div in document.DocumentNode.SelectNodes("//div[contains(@class,'listevent')]"))
{
}

If you are more familiar with css style selectors, try fizzler and do this

document.DocumentNode.QuerySelectorAll("div.listevent");