解析HTML - 仅显示一个不是列表的项目

时间:2014-09-25 19:40:29

标签: xpath html-agility-pack

    protected async override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        string htmlPage = "";
        using (var client = new HttpClient())
        {
       //     htmlPage = await client.GetStringAsync("http://m.buses.co.uk/stop.aspx?stopid=6884");

     //htmlPage = await client.GetStringAsync("http://www.imdb.com/movies-in-theaters/");
     htmlPage = await client.GetStringAsync("http://m.buses.co.uk/destinations.aspx");

        }

        HtmlDocument htmlDocument = new HtmlDocument();
        htmlDocument.LoadHtml(htmlPage);

        List<Movie> movies = new List<Movie>();
        foreach (var div in htmlDocument.DocumentNode.SelectNodes("//div[starts-with(@class, 'menu')]"))
        {
            Movie newMovie = new Movie();
      //  newMovie.Cover = div.SelectSingleNode(".//div[@class='image']//img").Attributes["src"].Value;
        //   newMovie.Title = div.SelectSingleNode(".//h4[@itemprop='name']").InnerText.Trim();
         // newMovie.Summary = div.SelectSingleNode(".//div[@class='outline']").InnerText.Trim();
           //   newMovie.Summary = div.SelectSingleNode(".//div[@class='services']").InnerText.Trim();

          newMovie.Summary = div.SelectSingleNode(".//a[starts-with(@href, 'place.aspx')]").InnerText.Trim();
            movies.Add(newMovie);
        }
        lstMovies.ItemsSource = movies;
    }

我想获得热门目的地名单,地名,下面是我感兴趣的部分。我能够使用上面的代码获得第一名 - 美国运通体育馆。但它不再显示了。

      <div class="menu">
    <ul>
        <li>
            <a href="place.aspx?placeid=1154">
            Amex Stadium
            </a>
           </li>

        <li>
            <a href="place.aspx?placeid=1136">
            Brighton Marina
            </a>
           </li>

        <li>
            <a href="place.aspx?placeid=907">
            Brighton Station
            </a>
           </li>

        <li>
            <a href="place.aspx?placeid=910">
            Brighton University  Moulsecoomb
            </a>
           </li>

        <li>
            <a href="place.aspx?placeid=916">
            Churchill Square
            </a>
           </li>

        <li>
            <a href="place.aspx?placeid=918">
            Coldean
            </a>
           </li>

        <li>
            <a href="place.aspx?placeid=924">
            County Hospital
            </a>
           </li>

        <li>
            <a href="place.aspx?placeid=943">
            Eastbourne
            </a>
           </li>

        <li>
            <a href="place.aspx?placeid=957">
            George Street  Hove
            </a>
           </li>

        <li>
            <a href="place.aspx?placeid=965">
            Hangleton
            </a>
           </li>

        <li>
            <a href="place.aspx?placeid=972">
            Hollingbury
            </a>
           </li>

        <li>
            <a href="place.aspx?placeid=993">
            Lewes
            </a>
           </li>

        <li>
            <a href="place.aspx?placeid=997">
            Longhill School
            </a>
           </li>

        <li>
            <a href="place.aspx?placeid=1006">
            Mile Oak
            </a>
           </li>

        <li>
            <a href="place.aspx?placeid=1011">
            Newhaven
            </a>
           </li>

        <li>
            <a href="place.aspx?placeid=1134">
            North Street
            </a>
           </li>

        <li>
            <a href="place.aspx?placeid=1020">
            Old Steine
            </a>
           </li>

        <li>
            <a href="place.aspx?placeid=1026">
            Patcham
            </a>
           </li>

        <li>
            <a href="place.aspx?placeid=1028">
            Peacehaven
            </a>
           </li>

        <li>
            <a href="place.aspx?placeid=1035">
            Portslade Station
            </a>
           </li>

        <li>
            <a href="place.aspx?placeid=1042">
            Queens Park
            </a>
           </li>

        <li>
            <a href="place.aspx?placeid=1047">
            Rottingdean
            </a>
           </li>

        <li>
            <a href="place.aspx?placeid=1057">
            Seaford
            </a>
           </li>

        <li>
            <a href="place.aspx?placeid=1062">
            Shoreham
            </a>
           </li>

        <li>
            <a href="place.aspx?placeid=1135">
            St Peter's Church
            </a>
           </li>

        <li>
            <a href="place.aspx?placeid=1074">
            Steyning
            </a>
           </li>

        <li>
            <a href="place.aspx?placeid=1076">
            Sussex University
            </a>
           </li>

        <li>
            <a href="place.aspx?placeid=1080">
            Tunbridge Wells
            </a>
           </li>

        <li>
            <a href="place.aspx?placeid=1082">
            Uckfield
            </a>
           </li>

        <li>
            <a href="place.aspx?placeid=1091">
            Westdene
            </a>
           </li>

        <li>
            <a href="place.aspx?placeid=1092">
            Whitehawk
            </a>
           </li>

        <li>
            <a href="place.aspx?placeid=1095">
            Woodingdean
            </a>
           </li>
    </ul>
    </div>

1 个答案:

答案 0 :(得分:0)

您的选择有误。声明

SelectNodes("//div[starts-with(@class, 'menu')]")

只选择<DIV class="menu">,因为只有一个这样的DIV,你只能得到一个地方。你应该把它改成:

SelectNodes("//div[starts-with(@class, 'menu')]/ul/li")

然后使用:

newMovie.Summary = div.SelectSingleNode("a[starts-with(@href, 'place.aspx')]").InnerText.Trim();

注意,我从后一个选项中删除了.//