8

我想使用 div 获取具有空间类名称和 em 子级的“li”上的远程 html 内容。

我的远程内容是这样的

<ul>

<li class="用户">

<div class="name">我的名字 1</div>

<div class="rep">20</div>

</li>

<li class="用户">

<div class="name">我的名字2</div>

<div class="rep">23</div>

</li>

<li class="用户">

<div class="name">我的名字 3</div>

<div class="rep">40</div>

</li>

</ul>

得到他们的数据后,它必须是这样的。

[我的名字 1,20]

[我的名字 2,23]

[我的名字 3,40]

谢谢。

对不起我可怜的英语

注意:在远程页面上有比这更多的内容。

4

1 回答 1

27

使用 CURL 读取远程 URL 以获取 HTML。

$url = "http://www.example.com";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($curl);
curl_close($curl);

然后使用PHP 的 DOM 对象模型来解析 HTML。

例如<h1>要从源中获取所有标签,

$DOM = new DOMDocument;
$DOM->loadHTML( $output);

//get all H1
$items = $DOM->getElementsByTagName('h1');

//display all H1 text
 for ($i = 0; $i < $items->length; $i++)
        echo $items->item($i)->nodeValue . "<br/>";
于 2012-07-31T05:54:28.617 回答