gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:21:48 25 4
gpt4 key购买 nike

我是 Xpath 新手。我想循环遍历 cURL 查询的结果并打印页面上唯一表的每个元素。

我使用了 Firefox 的 Xpath 插件来获取我的表达式,我的表格结构如下:

<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>

这是我迄今为止尝试过的代码。我收到一条消息“警告:为 foreach() 提供的参数无效”。我做错了什么?

$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;
}

最佳答案

假设您要查找的表实际上位于 <div id="divResults"> 中...

$tableRows = $xpath->query('//div[@id="divResults"]/table/tbody/tr');
foreach ($tableRows as $row) {
$cells = $row->getElementsByTagName('td');
}

关于php - 警告:DOMXPath::query():表达式无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19208213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com