簡體   English   中英

使用XPath檢索有序列表中的所有鏈接

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

如何使用XPath檢索作為有序列表中的列表項顯示的一組鏈接? 我需要使用以下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>

我必須檢索的部分鏈接已在上面的代碼中標記( <!-- get this link --> )。 我現在使用以下字符串來檢索所有列表項:

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

檢索所有列表項。 但是我不知道如何“輸入”每個列表項並檢索鏈接標簽的內容。 在我發現的示例中,必須了解列表項的數量, 然后才能訪問它們。 事實並非如此,因為該論壇可能具有不同數量的列表項(該模板用於論壇引擎,尤其不是論壇)。

如何檢索列表項中的所有鏈接?

嘗試在xpath下獲取URL:-

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

如果您希望所有的li都在我看來,那么xpath如下:

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

我認為以下是您期望的xpath:

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

希望它能對您有所幫助:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM