CsQuery:解析li项目中的li项目集合

时间:2015-09-07 12:12:40

标签: csquery

我对CSQuery比较陌生,我想在 li 中解析 li ,但没有成功。

然而,我成功地遍历了一个 li 中的项目,但并非所有 li 项目。

此外,无法控制 li 中的 li 项目数量。

<ol class="programs_epg_content_alldays_container">
  <li>
    <ol class="programs_epg_content_aDay before" style="top: -540px; left: -2838.33px;">
      <li title="Faites entrer l'accusé" style="width:85.333333333333px; left:0px" class="programs_epg_content_program ">
        <a href="/videos/127336360" class="programs_epg_replay" style="left:82.333333333333px">    </a>
        <div class="program-details" data-idx="0">
          <article id="program_55caca8e706765375d196802">
            <time datetime="2015-08-30">22:40    </time>
            <h4 title="Faites entrer l'accusé">Faites entrer l'accusé    </h4>
            <p title="La disparition de Christophe Dalmasso">La disparition de Christophe Dalmasso    </p>
            <p title="Magazine (1h45)">Magazine (1h45)    </p>
            <p>    </p>
          </article>
        </div>
      </li>
      <li title="Météo 2" style="width:3.6666666666667px; padding:9px 4px; left:108.33333333333px" class="programs_epg_content_program ">
        <div class="program-details" data-idx="1">
          <article id="program_55caca8e706765375d206802">
            <time class="hide" datetime="2015-08-31">00:25    </time>
            <p class="programs_epg_content_program_truncated">    </p>
          </article>
        </div>
      </li>
      <li title="Flashpoint" style="width:163.33333333333px; left:117px" class="programs_epg_content_program ">
        <a href="/videos/127341381" class="programs_epg_replay" style="left:160.33333333333px">    </a>
        <div class="program-details" data-idx="2">
          <article id="program_55caca8e706765375d276802">
            <time datetime="2015-08-31">00:27    </time>
            <h4 title="Flashpoint">Flashpoint    </h4>
            <p title="Contamination - Saison 4 - Episode 17">Contamination - Saison 4 - Episode 17    </p>
            <p title="Série et fiction (43mn)">Série et fiction (43mn)    </p>
            <p>    </p>
          </article>
        </div>
      </li>
    </ol>
  </li>
  <li>
    <ol class="programs_epg_content_aDay today" style="top: -540px; left: -2838.33px;">
      <li title="Un jour, un destin" style="width:17.333333333333px; padding:9px 4px; left:0px" class="programs_epg_content_program today">
        <div class="program-details" data-idx="0">
          <article id="program_55d557b3706765b328297902">
            <time class="hide" datetime="2015-09-06">22:35    </time>
            <p class="programs_epg_content_program_truncated">...    </p>
          </article>
        </div>
      </li>
      <li title="Météo 2" style="width:4px; padding:9px 4px; left:30.333333333333px" class="programs_epg_content_program today">
        <div class="program-details" data-idx="1">
          <article id="program_55d557b3706765b328307902">
            <time class="hide" datetime="2015-09-07">00:07    </time>
            <p class="programs_epg_content_program_truncated">.    </p>
          </article>
        </div>
      </li>
      <li title="Journal" style="width:20.333333333333px; left:1820px" class="programs_epg_content_program today">
        <div class="program-details" data-idx="16">
          <article id="program_55d557b4706765b328987902">
            <time class="hide" datetime="2015-09-07">07:00    </time>
            <p class="programs_epg_content_program_truncated">...    </p>
          </article>
        </div>
      </li>
    </ol>
  </li>
  <li>
    <ol class="programs_epg_content_aDay nextday" style="top: -540px; left: -2838.33px;">
      <li title="Cash investigation" style="width:258.66666666667px; left:0px" class="programs_epg_content_program ">
        <div class="program-details" data-idx="0">
          <article id="program_55d557b4706765b328947a02">
            <time datetime="2015-09-07">23:05    </time>
            <h4 title="Cash investigation">Cash investigation    </h4>
            <p title="Mon président est en voyage d'affaires">Mon président est en voyage d'affaires    </p>
            <p title="Magazine (2h00)">Magazine (2h00)    </p>
            <p>    </p>
          </article>
        </div>
      </li>
      <li title="Expression directe" style="width:8.6666666666667px; padding:9px 4px; left:281.66666666667px" class="programs_epg_content_program ">
        <div class="program-details" data-idx="1">
          <article id="program_55d557b4706765b3289b7a02">
            <time class="hide" datetime="2015-09-08">01:05    </time>
            <p class="programs_epg_content_program_truncated">..    </p>
          </article>
        </div>
      </li>
      <li title="Dans quelle éta-gère" style="width:8.6666666666667px; padding:9px 4px; left:303.33333333333px" class="programs_epg_content_program ">
        <div class="program-details" data-idx="2">
          <article id="program_55d557b4706765b328a27a02">
            <time class="hide" datetime="2015-09-08">01:10    </time>
            <p class="programs_epg_content_program_truncated">..    </p>
          </article>
        </div>
      </li>
    </ol>
  </li>
</ol>

我正在尝试使用CSQuery循环遍历每个li项目以获得以下结果:

Yesterday   Faites entrer l'accusé  22:40   2015-08-30  
Yesterday   Météo 2 00:25   2015-08-31  
Yesterday   Flashpoint  00:27   2015-08-31  
Today   Un jour, un destin  22:35   2015-09-06  
...

我只能通过以下方式(即今天的节目):

string xPathText = "section.programs_epg_content ol.programs_epg_content_alldays_container li ol.programs_epg_content_aDay.today li";
        var neededItems = dom[xPathText];
        foreach (var item in neededItems)

非常感谢你的帮助。

更新 以下就是诀窍。

string xPathText = "section.programs_epg_content ol.programs_epg_content_alldays_container li";
        var neededItems = dom[xPathText];
        foreach (var item in neededItems)
        {
            var myItems = CQ.Create(item)["ol li"];
            foreach (var item1 in myItems)
            {
                string startTime = CQ.Create(item1)["div.program-details article time"].Text();
                string broadcastDay = CQ.Create(item1)["div.program-details article time"].Attr("datetime");
                string TVGTitle = CQ.Create(item1)["div"].Parent().Attr("title");
                string episodeTitleTemp = CQ.Create(item1)["div.program-details figcaption p:first"].Text();
                episodeTitleTemp = Regex.Replace(episodeTitleTemp, "(Magazine|Documentaire|Divertissement|Info / JT) [(].+[)]", " ");
                context.Response.Write("\"" + startTime + "\",\"" + TVGTitle + "\",\"" + episodeTitleTemp + "\",\"" + broadcastDay + "\"");
                context.Response.Write(Environment.NewLine);
            }
        }

0 个答案:

没有答案