【问题标题】:PHP DOMXPath extract href of anchor inside a tdPHP DOMXPath 提取 td 内锚点的 href
【发布时间】:2018-04-23 12:23:03
【问题描述】:

使用 PHP DOMXPath 我需要获取包含在 td 中的锚点的“href”。 我已经能够获得所有正确的 xPath 来到达 td 并且我可以得到里面的文本,但我无法理解如何提取锚点。 对于我的其他需要,我必须提取所有 tr 作为第一步,所以我当前的代码如下:

$xpath = new DOMXPath($dom);
$trList = $xpath->query('//div[@id="main_content"]/table/tr/td/table[3]/tr[2]/td/table/tr');
$rowToSkip = 1;
foreach($trList as $rowNum => $row){        
        if($rowNum <= $rowToSkip){
            continue;
        }
        $cols = $row->childNodes;
        $dataList[($rowNum-$rowToSkip)]['number'] = preg_replace("/[^0-9]/", "", strip_tags($cols->item(2)->nodeValue));
}

如何检索href?

我也试试

$cols->item(2)->attributes->getNamedItem("href")->nodeValue

但没有运气

下面是与原版完全相同的 HTML 示例:

<div id="main_content">
<table class="wrapper" border="0" cellspacing="0" cellpadding="0">
        <tr>
            <td>
                <table border="0" cellspacing="0" cellpadding="0" id="breadcrumb">
                        <tr>
                            <td class="breadcrumb">
                                <a href="" class="breadcrumb">head link</a>
                                <a href="" class="breadcrumb">head link</a>
                            </td>
                        </tr>
                </table>
                <div><img src="space.gif" width="1" height="7" alt="" border="0"></div>                    
                <table border="0" cellspacing="0" cellpadding="0" width="100%">
                        <tr>
                            <td colspan="5" >test</td>
                        </tr>
                        <tr>
                            <td colspan="5"></td>
                        </tr>
                </table>
                <div><img width="1" height="32" border="0" alt="" src="space.gif"></div>
                <table border="0" cellpadding="0" cellspacing="0" width="100%">
                        <tr>
                            <td width="100%" >test 02</td>
                        </tr>
                        <tr>
                            <td>
                                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                        <tr>
                                            <td nowrap="nowrap" colspan="8">header col 1</td>
                                            <td nowrap="nowrap" colspan="5">header col 2</td>
                                        </tr>
                                        <tr>
                                            <td nowrap="nowrap">
                                                <a href="" >test col 0</a>
                                            </td>
                                            <td  nowrap="nowrap">
                                                <a href="" >test col 1</a>
                                            </td>
                                            <td  nowrap="nowrap">test col 2</td>
                                            <td  nowrap="nowrap">
                                                <a href="" >test col 3</a>
                                            </td>
                                            <td  nowrap="nowrap">
                                                <a href="" >test col 4</a>
                                            </td>
                                            <td  nowrap="nowrap">
                                                <a href="" >test col 5</a>
                                            </td>
                                            <td  nowrap="nowrap">test col 6</td>
                                            <td  nowrap="nowrap">test col 7</td>
                                            <td  nowrap="nowrap">test col 8</td>
                                            <td  nowrap="nowrap">test col 9</td>
                                            <td  nowrap="nowrap">test col 10</td>
                                            <td  nowrap="nowrap">test col 11</td>
                                            <td  nowrap="nowrap">test col 12</td>
                                        </tr>
                                        <tr>
                                            <td  nowrap="nowrap" rowspan="1">
                                                <a href="" >detail info col 0</a>
                                            </td>
                                            <td  nowrap="nowrap" rowspan="1" style="background-color:red">
                                                <a href="" >detail info col 1 this is needed column</a>                                                    
                                            </td>
                                            <td  nowrap="nowrap" rowspan="1">
                                                <a href="" >detail info col 2</a>
                                            </td>
                                            <td  nowrap="nowrap" rowspan="1">
                                                <a href="" >detail info col 3</a>
                                            </td>
                                            <td  nowrap="nowrap" rowspan="1">
                                                <a href="" >detail info col 4</a>
                                            </td>
                                           <td  nowrap="nowrap" rowspan="1">
                                                <a href="" >detail info col 5</a>
                                            </td>
                                            <td  nowrap="nowrap" rowspan="1">
                                                <a href="" >detail info col 6</a>
                                            </td>
                                            <td  nowrap="nowrap" rowspan="1">
                                                <a href="" >detail info col 7</a>
                                            </td>
                                            <td  nowrap="nowrap" rowspan="1">
                                                <a href="" >detail info col 8</a>
                                            </td>
                                            <td  nowrap="nowrap" rowspan="1">
                                                <a href="" >detail info col 9</a>
                                            </td>
                                            <td  nowrap="nowrap" rowspan="1">
                                                <a href="" >detail info col 10</a>
                                            </td>
                                            <td  nowrap="nowrap" rowspan="1">
                                                <a href="" >detail info col 11</a>
                                            </td>
                                            <td  nowrap="nowrap" rowspan="1">
                                                <a href="" >detail info col 12</a>
                                            </td>
                                        </tr>
                                </table>
                            </td>
                        </tr>
                </table>
            </td>
        </tr>
</table>

【问题讨论】:

  • html 是缩减版还是精确版(意味着 id xy 下的一个 a-tag)?如果是后者,您可以直接定位 a-tag 而无需明确计算表和行。
  • 类似:$xpath-&gt;query('//*[@id="main_content"]//@href');
  • @Yoshi 是我在 div 内有十几个具有相同表结构的块的页面的摘录。我需要准确提取此路径,因为页面内有十几个链接.. 遗憾的是,这是定位正确链接的唯一方法
  • 好的,双表(关闭最后的a-tag)是故意的吗?

标签: php domxpath


【解决方案1】:

使用您发布的结构,以下输出 href-value:

<?php
$dom = new DOMDocument('1.0');
$dom->loadHTMLFile('input.html');

$xpath = new DOMXPath($dom);

$query = '//*[@id="main_content"]/table/tr/td/table[3]/tr[2]/td/table/tr[position() >= 3]/td[2]/a';

$nodes = $xpath->query($query);

foreach ($nodes as $node) {
    /** @var $node DOMElement */
    var_dump(
        $node->getAttribute('href'), // the href-attribute value
        $node->nodeValue // the inner text
    );
}

【讨论】:

  • 好的,这项工作,但我无法检索我在示例中获得的文本(数字“123456”)由 dataList[($rowNum-$rowToSkip)]['number'] 检索= preg_replace("/[^0-9]/", "", strip_tags($cols->item(2)->nodeValue));以这种方式..可能我错过了一些东西..
  • 好的,错过了。所以你想要 href 属性值和内部文本值?
  • 使用新的 xPath 我得到所有列链接,我只需要一个特定的列,我用只有一行的完整整页更新示例。这是页面的剪切和过去,仅删除了列名。我需要名为“
    这最后一个查询似乎正是 chrome 生成的 xPath。通过这个查询我只能得到第一行的值。我错过了什么?
  • @Marco 我很困惑,代码准确地选择了您想要的 a-tag 并显示了如何访问 href 属性和内部文本。 “...只有第一行值...”是什么意思
猜你喜欢
相关资源
最近更新 更多
热门标签