dra8603 2013-10-06 11:11
浏览 378
已采纳

警告:DOMXPath :: query():表达式无效

I'm an Xpath newbie. I want to loop through the result of a cURL query and print each element of the only table on the page.

I've used the Xpath plugin for Firefox to obtain my expression and my table is structured as follows:

<table>
<tr class="listItemOneBg">
    <td valign="top">
        SMITH
    </td>
    <td valign="top">
        WILLIAM C C
    </td>
    <td valign="top">
        Male
    </td>
    <td valign="top">

    </td>
    <td valign="top">

    </td>
    <td valign="top">

    </td>
    <td valign="top">

    </td>
    <td valign="top">
        BLACKWOOD
    </td>
    <td valign="top">
        61
    </td>
    <td valign="top">
        1924
    </td>
    <td valign="top">
        <a target="_blank" href='XXX'>
            order</a>
    </td>
</tr>

<tr class="listItemTwoBg">
    <td valign="top">
        SMITH
    </td>
    <td valign="top">
        WILLIAM C PAGE-
    </td>
    <td valign="top">
        Male
    </td>
    <td valign="top">

    </td>
    <td valign="top">

    </td>
    <td valign="top">

    </td>
    <td valign="top">

    </td>
    <td valign="top">
        SWAN
    </td>
    <td valign="top">
        9
    </td>
    <td valign="top">
        1914
    </td>
    <td valign="top">
        <a target="_blank" href='XXY'>
            order</a>
    </td>
</tr>       

Here's the code I've tried so far. I get a message"Warning: Invalid argument supplied for foreach()". What am I doing wrong?

$page = curl_exec($ch);
curl_close($ch);

// Create new PHP DOM document
$dom = new DOMDocument;
// Load html from curl request into document model
@$dom->loadHTML($page);
$xpath = new DOMXPath($dom);

$tableRows = $xpath->query("id('divResults')/table/tbody/tr");
foreach ($tableRows as $row) {
     // fetch all 'tds' inside this 'tr'
    $td = $xpath->query('td', $row);
    echo $td->item(1)->textContent;
}
  • 写回答

2条回答 默认 最新

  • dpje52239 2013-10-07 03:13
    关注

    Assuming the table you're after is actually in a <div id="divResults">...

    $tableRows = $xpath->query('//div[@id="divResults"]/table/tbody/tr');
    foreach ($tableRows as $row) {
        $cells = $row->getElementsByTagName('td');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里