Question

<article class="four columns">
<header class="four columns alpha">
    <h2 class="trunker">
        <span id="MainContentPlaceHolder_ctl12_ctl16_MovieTitleH2" title="En du elsker">En du elsker</span>
    </h2>
    <hr />
</header>
<figure class="two columns alpha">


    <div id="MainContentPlaceHolder_ctl12_ctl16_insertVignetTop"></div>
    <div id="MainContentPlaceHolder_ctl12_ctl16_insertVignetBottom"></div>
    <div id="MainContentPlaceHolder_ctl12_ctl16_insertTrailer"><div id="playerPoster" class="playTrailer" name="14532" onclick="x=1;"></div></div>
    <a id="MainContentPlaceHolder_ctl12_ctl16_MovieDetailsHyperLink" title="En du elsker" href="MovieDetails.aspx?movieId=3383"><img src="http://mother.poweredbyintegra.dk/posters/enduelsker_hoej_m.jpg" id="MainContentPlaceHolder_ctl12_ctl16_ImageUrlImg" /></a>
</figure>
<div id="MainContentPlaceHolder_ctl12_ctl16_ShowTimesDiv" class="two columns omega">

<span class="ticket"><input name="ctl00$MainContentPlaceHolder$ctl12$ctl16$ctl03" type="button" class="ticket" value="Læs mere" onclick="location.href=&#39;MovieDetails.aspx?movieId=3383&#39;" /></span><span class="ticket"><input name="ctl00$MainContentPlaceHolder$ctl12$ctl16$ctl04" type="button" class="ticket" value="18:30" onclick="location.href=&#39;OrderMovieTicket.aspx?showId=11837&#39;" /></span></div>

I need to get title="En du elsker" and link href="MovieDetails.aspx?movieId=3383" to work together I want it to do the same for the same next 3.

This is how I tryed:

@using HtmlAgilityPack;

@{
HtmlWeb hw = new HtmlWeb(); 
hw.AutoDetectEncoding = true;
hw.OverrideEncoding = System.Text.Encoding.GetEncoding("ISO-8859-1");

HtmlDocument doc = hw.Load("ronnebio.dk/NextDaysProgramme.aspx?offset=0");

 //doc.DetectEncodingAndLoad(
 List<string> temp = new List<string>();
 int count = 1;

foreach(HtmlNode link in doc.DocumentNode.SelectNodes("//div[@class='inner clearfix']"))
{
    if (count > 3)
    {
        break; 
    }

    string linkhref = link.GetAttributeValue("href", "");
    string titel = link.InnerText;
        if (linkhref != "" 
        && linkhref.Contains("MovieDetails.aspx")
        && !temp.Contains(titel))
    {
        temp.Add(titel);
        count++;

        <div class="nyhedlink"><a href="@linkhref" target="_blank">- @titel</a></div>
    }
}
}

I cant find the problem? hope you can solve the isue- THX

Was it helpful?

Solution

Accord the URL you have posted with the following query you can obtain only the firts three a tags in the following way :

var list = (from item in doc.DocumentNode.Descendants("a")
                       where
                           item.ParentNode.Name.Equals("figure") &&
                           item.ParentNode.Attributes["class"].Value == "two columns alpha"
                       select new
                              {
                                  Title = item.Attributes["title"].Value,
                                  Link = item.Attributes["href"].Value
                              }).Take(3);

and the you can do it this :

foreach (var item in list)
{
    /* for example */
    <div class="nyhedlink"><a href="@item.Link" target="_blank">- @item.Title</a></div>       
}

OTHER TIPS

Thx m8... still a bit unsesure of where to put the first code you wrote.. This way it finds only one txt but keeps repeating it tons of times. i need only the first 3 titles and href.

 @using HtmlAgilityPack;

@{
    HtmlWeb hw = new HtmlWeb(); 
    hw.AutoDetectEncoding = true;
    hw.OverrideEncoding = System.Text.Encoding.GetEncoding("ISO-8859-1");

    HtmlDocument doc = hw.Load("http://ronnebio.dk/NextDaysProgramme.aspx?offset=0");

     //doc.DetectEncodingAndLoad(
     List<string> temp = new List<string>();
     int count = 1;

    var element = (from item in doc.DocumentNode.Descendants("a")
               where item.Id == "MainContentPlaceHolder_ctl12_ctl16_MovieDetailsHyperLink"
               select new
                      {
                          Title = item.Attributes["title"].Value,
                          Link = item.Attributes["href"].Value
                      }).First();

            <div class="nyhedlink"><a href="@element.Link" target="_blank">- @element.Title</a></div>

        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top