【发布时间】:2015-02-14 09:55:09
【问题描述】:
我想解析 HTML 页面以从表中获取数据(基本上我想遍历所有 tr 标签)。
我有下一个问题:
- 如何在表头跳过
tr? - 如何获取
td标签的onclick属性值? - 如何在每个
tr中计算td
HTML 结构:
<tr>
<td onclick="window.location='home.php?navi=148';">kkkk</td>
<td>demo</td>
<td>kkkk</td>
</tr>
我想获得 window.location='home.php?navi=148'; 我正在使用的代码:
$url = $html;
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$html = curl_exec($ch);
curl_close($ch);
$dom = new DOMDocument();
@$dom->loadHTML($html);
# Iterate over all the <a> tags
foreach($dom->getElementsByTagName('td') as $link) {
# Show the <a href>
print_r($link);
echo "<br />";
}
【问题讨论】: