【问题标题】:How to get Content of Remote HTML page如何获取远程 HTML 页面的内容
【发布时间】:2012-07-28 20:26:15
【问题描述】:

我想使用 div 获取“li”上的远程 html 内容,该内容具有特定的类名和 em 的子项。

我的远程内容是这样的

  • 我的名字 1

    20

  • 我的名字2

    23

  • 我的名字 3

    40

拿到他们的数据后一定是这样的。

[我的名字 1,20]

[我的名字 2,23]

[我的名字 3,40]

谢谢。

对不起我的英语不好

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

【问题讨论】:

    标签: php html


    【解决方案1】:

    使用 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's DOM object model解析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/>";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-21
      • 1970-01-01
      • 1970-01-01
      • 2012-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多