使用CsQuery遍历dom [英] Traverse the dom with CsQuery

查看:156
本文介绍了使用CsQuery遍历dom的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习如何使用CsQuery遍历dom以获取特定文本.

I'm trying to learn how to use CsQuery to traverse a dom to get specific text.

html看起来像这样:

The html looks like this:

<div class="featured-rows">
  <div class="row">
    <div class="featured odd" data-genres-filter="MA0000002613">
      <div class="album-cover">
      <div class="artist">
        <a href="http://www.allmusic.com/artist/half-japanese-0000555654">Half apanese</a>
      </div>
      <div class="title">
      <div class="label"> Joyful Noise </div>
      <div class="styles">
      <div class="rating allmusic">
      <div class="rating average">
      <div class="headline-review">
    </div>
    <div class="featured even" data-genres-filter="MA0000002572, MA0000002613">
    </div>
  <div class="row">
  <div class="row">
  <div class="row">

我的代码尝试如下:

public void GetRows()
        {
            var artistName = string.Empty;
            var html = GetHtml("http://www.allmusic.com/newreleases");            
            var rows = html.Select(".featured-rows");
            foreach(var row in rows)
            {     
                var odd = row.Cq().Find(".featured odd");
                foreach(var artist in odd)
                {
                    artistName = artist.Cq().Text();
                }
            }
        }

.featured-row作品的第一个选择,但后来我不知道该如何选择.artist来获取文本.

The first select for .featured-row works but then i don't know how to get down to the .artist to get the text.

推荐答案

List<string> artists = html[".featured .artist a"].Select(dom=>dom.TextContent).ToList();

其中html ==您的CQ对象.

where html == your CQ object.

var odd = row.Cq().Find(".featured odd");

应该是

var odd = row.Cq().Find(".featured.odd");

这篇关于使用CsQuery遍历dom的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆