简体   繁体   English

使用XPath检索有序列表中的所有链接

[英]Retrieving all links within an ordered list using XPath

How can I retrieve a set of links present as list items in an ordered list using XPath? 如何使用XPath检索作为有序列表中的列表项显示的一组链接? I need to retrieve all sections from a forum with the following html code: 我需要使用以下html代码从论坛中检索所有部分:

<div id="pagewrapper" class="fixed">
<div id="toplinks" class="toplinks" style="position: relative; top: 145px;">
<div class="above_body" style="height: 210px;">
<div class="body_wrapper">
<div id="breadcrumb" class="breadcrumb">
<div id="pagetitle">
<ol id="forums" class="floatcontainer">
<li id="cat3" class="forumbit_nopost new L1">
<div class="forumhead tcat foruminfo L1 collapse">
<div class="tbody_left">
<div class="tbody_right">
<ol id="c_cat3" class="childforum">
<li id="forum9" class="forumbit_post new L2">
<div class="forumrow table">
<div class="foruminfo td" style="padding-top: 12px; padding-bottom: 12px;">
<img id="forum_statusicon_9" class="forumicon" alt="" src="elitex360/statusicon/forum_new-48.png">
<div class="forumdata">
<div class="datacontainer">
<div class="titleline">
<h2 class="forumtitle">
<a href="https://forums.com/forum/index">Forum index</a> <!-- get this link -->
</h2>
</div>
<p class="forumdescription">
</div>
</div>
</div>
<h4 class="nocss_label">Forum Actions:</h4>
<h4 class="nocss_label">Forum Statistics:</h4>
<ul class="forumstats td" style="padding-top: 18px; padding-bottom: 12px;">
<div class="forumlastpost td">
</div>
</li>
<li id="forum22" class="forumbit_post new L2">
<li id="forum40" class="forumbit_post new L2">
</ol>
<div class="tbody_under"></div>
</div>
</div>
<div class="tfoot">
</li>
<li id="cat4" class="forumbit_nopost new L1">
<li id="cat52" class="forumbit_nopost new L1">
<li id="cat5" class="forumbit_nopost new L1">
<li id="cat6" class="forumbit_nopost new L1">
<li id="cat7" class="forumbit_nopost old L1">
</ol>

The section links I have to retrieve are marked in the code above ( <!-- get this link --> ). 我必须检索的部分链接已在上面的代码中标记( <!-- get this link --> )。 I am now using the following string to retrieve all list items: 我现在使用以下字符串来检索所有列表项:

//div[@id='pagewrapper']/div[3]/ol

retrieving all list items. 检索所有列表项。 But I don't know how to "enter" each list item and retrieve the link label's contents. 但是我不知道如何“输入”每个列表项并检索链接标签的内容。 In the examples I found, knowledge of the number of list items is necessary prior to accessing them. 在我发现的示例中,必须了解列表项的数量, 然后才能访问它们。 That is not the case as the forum may have a different number of list items (the template is for a forum engine, not a forum in particular). 事实并非如此,因为该论坛可能具有不同数量的列表项(该模板用于论坛引擎,尤其不是论坛)。

How can I retrieve all links within the list items? 如何检索列表项中的所有链接?

Try below xpath to get the URL:- 尝试在xpath下获取URL:-

//a[contains(.,'Forum index')]/@href

If you want all li in ol as I understand from you question then the xpath is as below:- 如果您希望所有的li都在我看来,那么xpath如下:

//div[@id='pagewrapper']//li[@id='cat3']//ol//li

I think Below is the xpath you are expecting:- 我认为以下是您期望的xpath:

   //div[@id='pagewrapper']//div/@href

Hope it will help you :) 希望它能对您有所帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM