如何获得远程HTML页面的内容 [英] How to get Content ot Remote HTML page

查看:63
本文介绍了如何获得远程HTML页面的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要获得在li上使用div来区分类名和em的孩子的远程HTML内容。 我的远程内容就像这样





  • 我的名字1



    20





  • 我的名字2

    23



    $ b

  • div class =name> My Name 3



40




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


[我的名字1,20]



[我的名字2,23]



[我的名字3,40]


感谢。



对不起My Poor English



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

解决方案

使用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);

//获取所有H1
$ items = $ DOM-> getElementsByTagName('h1');

//显示所有的H1文本
($ i = 0; $ i< $ items->长度; $ i ++)
echo $ items-> item ($ i) - > nodeValue。 <峰; br /> 中;


I want to get remote html contents that on "li" that with a spacific Class name and Children of em using divs.

My remote content is Like this

<ul>

<li class="user">

<div class="name">My Name 1</div>

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

</li>

<li class="user">

<div class="name">My Name 2</div>

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

</li>

<li class="user">

<div class="name">My Name 3</div>

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

</li>

</ul>

After get their data it must be like this.

[My Name 1,20]

[My Name 2,23]

[My Name 3,40]

Thanks.

Sorry for the My Poor English

Note : Have more content than this on remote page.

解决方案

Use CURL to read the remote URL to fetch the HTML.

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

Then use PHP's DOM object model to parse the HTML.

For example to fetch all <h1> tags from the source,

$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/>";

这篇关于如何获得远程HTML页面的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆