【发布时间】:2016-12-17 10:26:49
【问题描述】:
我正在使用 file_get_contents 从网站获取 html。我在 html 中有一个表(带有类名),我想在 html 标记中获取数据。
这就是我从 url 获取 html 数据的方式:
$url = 'http://example.com';
$content = file_get_contents($url);
html 看起来像:
<table class="space">
<thead></thead>
<tbody>
<tr>
<td class="marsia">1</td>
<td class="mars">
<div>Mars</div>
</td>
</tr>
<tr>
<td class="earthia">2</td>
<td class="earth">
<div>Earth</div>
</td>
</tr>
</body>
</table>
有没有办法像在 jQuery 中一样在 php 中搜索 DOM 元素?这样我就可以在第二个 td 中访问值 1、2(第一个 td)和 div 的值。
类似
a) 在 html 中搜索具有类名称空间的表
b) 在该表内部,在 tbody 内部,返回每个 tr 的“第一个 td 值”和“div 在第二个 td 内的值”
所以我明白了; 1 和火星,2 和地球。
【问题讨论】:
-
使用DOMDocument解析HTML。
标签: php html dom file-get-contents