【问题标题】:Simple HTML DOM parsing isnt working简单的 HTML DOM 解析不起作用
【发布时间】:2017-07-29 17:49:05
【问题描述】:

我正在尝试在我的本地主机上学习 HTML DOM 解析。但我一开始就崩溃了。

1) 我做了一个新项目

2) 我从simplehtmldom.sourceforge.net下载了文件

3) 我已将此代码放入我的 HTML 项目中

  <!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        // put your code here
         $html = file_get_html('http://www.google.com/');
        ?>
    </body>
</html>

4) 运行脚本时出现此错误:

致命错误:未捕获的错误:调用 C:\xampp\htdocs\PhpProject1\index.php:15 中未定义的函数 file_get_html() 堆栈跟踪:#0 {main} 在 C:\xampp\htdocs\PhpProject1\ 中抛出第 15 行的 index.php

我是不是误会了什么?

【问题讨论】:

  • 你需要使用DOM解析器。详情here
  • @AshiqurRahman 我已经下载了这些文件并将它们放在我的项目文件夹中
  • 您需要在 HTML 代码中包含 DOM 解析器 PHP 库文件。
  • 你必须下载并包含php dom解析器https://sourceforge.net/projects/simplehtmldom/files/

标签: php html parsing sourceforge


【解决方案1】:

我认为 PHP DOM Parser Library 不再支持 PHP 最新版本 所以,你需要使用 cURL 来帮助你。

<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://www.google.co.in");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
print $result;
?>

试试这个..!

【讨论】:

    【解决方案2】:

    file_get_html 不属于php

    要使用它,您需要下载并包含PHP Simple HTML DOM Parser

    那你就可以这样使用了

    include_once('simple_html_dom.php');
    $html = file_get_html('http://www.google.co.in');
    

    【讨论】:

      【解决方案3】:

      您需要使用 PHP 包含函数在 HTML 代码中包含 DOM 解析器 PHP 库文件。

      <html>
          <head>       
              <meta charset="UTF-8">
              <title></title>
          </head>
          <body>
              <?php
                 include_once('domparser.php');// use your file location 
              // put your code here
               $html = file_get_html('http://www.google.com/');
              ?>
          </body>
      </html>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多