doulu5109 2019-06-05 18:27
浏览 210
已采纳

从特定表中提取链接

I have a html code with many html tables. I want to extract links from specific one which has specific div above.

Here's my sample code:

        <div class="boxuniwersal_header">Table 1</div>
        <img src="img/boxuniwersal_top.gif" width="210" height="18" alt="" style="margin-top: 5px" />
        <div class="boxuniwersal_content">
          <div class="boxuniwersal_subcontent">
            <div class='menu_m1'><table cellpadding="3"><tr><td><a href="http://link.com"><img src="some.jpg"  width="45" /></a></td><td><a href="http://link.com">Some text</a></td></tr></table></div>
            <br />
          </div>
        </div>
        <!-- /box  -->

        <!-- box  -->
        <div class="boxuniwersal_header">Table 2</div>
        <img src="img/boxuniwersal_top.gif" width="210" height="18" alt="" style="margin-top: 5px" />
        <div class="boxuniwersal_content">
          <div class="boxuniwersal_subcontent">
            <div class='menu_m1'><table cellpadding="3"><tr><td><a href="http://link2.com"><img src="some2.jpg"  width="45" /></a></td><td><a href="http://link2.com">Some text2</a></td></tr></table></div>
            <br />
          </div>
        </div>
$domXPath = new DOMXPath($domDocument);
$results = $domXPath->query("//div/div/table/tr/td/a|//table//tr/td//a"); //querying domdocument
foreach($results as $result)
{
    $links[]=$result->getAttribute("href");
}

This code returns all links. I want to grab only links from Table1. Is it possible?

  • 写回答

1条回答 默认 最新

  • douying6206 2019-06-05 18:59
    关注

    Your main problem is just tuning the XPath expression to select the right XML.

    If you change your XPath to

    //div[text()="Table 1"]/following-sibling::div[1]//table//a
    

    What this does is first find the <div> element whose text is the one your after.

    The following-sibling::div[1] part will look at the first <div> element at the same level as the <div> element already selected (this is the one where the <table> is).

    The last part just looks for all <a> elements within the enclosing <table>.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line