0

下面是我的 HTML

<ul><li class="section">BROADCASTING</li>
<ul>
<li class="subsection"></li>
<li class="circle"><a href="/article/95242-STATION_BREAK.php">STATION BREAK</a></li>
<li class="circle"><a href="/article/98142-Labor_pains_hunger_pangs.php">Labor pains, hunger pangs</a></li>
<li class="circle"><a href="/article/101509-Wake_up_call_for_Dream_Team.php">Wake-up call for Dream Team</a></li>
<li class="circle"><a href="/article/136139-News_crew_turns_rescuer.php">News crew turns rescuer</a></li>
<li class="circle"><a href="/article/136140-Chopper_safety_had_been_challenged.php">Chopper safety had been challenged</a></li>
<li class="circle"><a href="/article/136142-Nielsen_adds_Dayton_.php">Nielsen adds Dayton..</a></li>
<li class="circle"><a href="/article/136143-Mondale_watch.php">Mondale watch</a></li>
<li class="circle"><a href="/article/136144-Those_70s_clearances.php">Those 70s clearances</a></li>
<li class="circle"><a href="/article/136145-Oscar_goes_to_ABC.php">Oscar goes to ABC</a></li>
<li class="circle"><a href="/article/136146-Hearst_Argyle_gives_a_green_light.php">Hearst-Argyle gives a green light</a></li>
<li class="circle"><a href="/article/136147-Finding_Geena_Davis.php">Finding Geena Davis</a></li>
<li class="circle"><a href="/article/136148-Syndication_Wrap_up.php">Syndication Wrap-up</a></li>
<li class="circle"><a href="/article/136149-CBS_TV_news_pioneer_dies_at_86.php">CBS TV news pioneer dies at 86</a></li>
<li class="circle"><a href="/article/136150-New_York_anchor_remembered.php">New York anchor remembered</a></li>
<li class="circle"><a href="/article/136151-News_sharing_in_West_Virginia.php">News sharing in West Virginia</a></li>
<li class="circle"><a href="/article/136152-News_dropping_in_Orlando.php">News dropping in Orlando</a></li>
<li class="subsection">Null</li>
<li class="circle"><a href="/article/136141-GET_WITH_THE_PROGRAM.php">GET WITH THE PROGRAM</a></li>
<li class="subsection">PEOPLE'S CHOICE</li>
<li class="circle"><a href="/article/97423-Syndication_as_branding.php">Syndication as branding</a></li>
</ul>
</ul>

现在我想分开部分和小节并获取小节内的网址。我已经去了每个部分

现在根据 HTML 结构,部分在单独的 li 中,在子部分打开之前关闭,所以一旦我到达部分,我必须移回 ul 然后必须继续获取子部分

但是,当我尝试../返回 ul (前一节)时,它说ivalid token

这就是我试图回到父级的原因。

HtmlNodeCollection sections = doc.DocumentNode.SelectNodes("//ul/li[@class='section'][contains(text(), 'BROADCASTING')]../ul");

但这不起作用

谁能告诉我哪里出错了,这将是一种帮助。

4

1 回答 1

1

您需要稍微更改一下 xpath 表达式:

var subsections = doc.DocumentNode.SelectNodes("//ul/ul[preceding-sibling::li[@class='section' and .='BROADCASTING']]/li[@class='subsection']");

它搜索作为先前兄弟的<ul>内部<ul><li class="section">BROADCASTING</li>然后在其中搜索<li class="subsection">

所以上面的代码选择了section下的所有BROADCASTING小节。

希望这可以帮助。

于 2013-10-21T21:24:58.793 回答