简体   繁体   中英

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM