ruqo
ruqo

Reputation: 65

How can I get specific links in a class?

I have a html structure like this:

<div class="YazarDetayTarih_Conteiner">
    <div class="YazarDetayTarih FL">30.Mart.2013, Cumartesi</div>
    <div class="YazarDetayBaslik FL">
<a class="haberlink"  href="http://www.hurriyet.com.tr/yazarlar/22928436.asp">Böyle özür olmaz Serdar Ortaç</a>
       </div>
     </div>
<div class="YazarDetayTarih_Conteiner">
<div class="YazarDetayTarih_Conteiner">

There are a couple div class="YazarDetayTarih_Conteiner"> . and I want to get these href links. Currently when I write like

HtmlElementCollection col = web.Document.GetElementsByTagName("a");
foreach (HtmlElement el in col)
{
       link = el.GetAttribute("href");
}

It gives all a href links at the page. How can I specifically take only href which belongs to \a class="haberlink"

edit : I couldnt make it work. after I try richTextBox1.Text += el.GetAttribute("class") it gives blank page.

while using nodes we can do like SelectNodes("//*[contains(@class,'haberlink')]"); is there any way to do this?

Upvotes: 0

Views: 56

Answers (1)

Tomas Grosup
Tomas Grosup

Reputation: 6514

Inside your foreach loop, skip the ones not having the desired class:

if(el.GetAttribute("class") != "haberlink")
   continue;

Upvotes: 1

Related Questions