【问题标题】:Dont Parse Simple HTML DOM on php 7.1不要在 php 7.1 上解析简单的 HTML DOM
【发布时间】:2018-04-23 04:32:09
【问题描述】:

我在 php 7.1 上运行简单的 html dom。

但是第一行我无法解析html

我的代码

<?php
include 'simple_html_dom.php';

$html = file_get_html('http://google.com');

echo $html;
?>

上面的代码页面什么都不显示(白色背景)。

但下面的代码却运行:

<?php
include 'simple_html_dom.php';
//base url
$base = 'https://google.com';
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_URL, $base);
curl_setopt($curl, CURLOPT_REFERER, $base);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$str = curl_exec($curl);
curl_close($curl);
// Create a DOM object
$html_base = new simple_html_dom();
// Load HTML from a string
$html_base->load($str);
echo $html_base;
$html_base->clear(); 
unset($html_base);
?>

然后,我尝试在上面的代码中使用下面的类来获取 img,但没有工作:

要获取的图片html:

<div class="product_thumb">
<a title="Me Before You" class="image-border" href=/me-before-you-a-novel-movie-tie-in-p69988.html">
<img class="   pict lazy-img" id="det_img_00069988" 
src="/images/thumbnails/product/115x/222614_me-before-you-a-novel-movie-tie-
in.jpg">
</a></div>

我的简单 HTML DOM,所有都不起作用(在可能的页面上没有 html)

//* Find all images 1st code
foreach($html_base->find('img[class=   pict lazy-img]') as $element) 
   echo '<img src="' . $element->src . '" />' . '<br>';
//* Find all images 2nd code
foreach($html_base->find('img[class=   pict lazy-img]',0) as $element) 
   echo '<img src="' . $element->src . '" />' . '<br>';
//* Find all images 3rd code
foreach($html_base->find('img[class$=pict lazy-img]',0) as $element) 
   echo '<img src="' . $element->src . '" />' . '<br>';
//* Find all images 4th code
foreach($html_base->find('img[class$=pict lazy-img]',0) as $element) 
   echo '<img src="' . $element->src . '" />' . '<br>';

【问题讨论】:

  • file_get_html 似乎返回一个对象,使用var_dump($html) 而不是echo
  • PHP ini file_get_contents external url - 这可能只是 allow_url_fopen PHP 配置。但是,您可以启用错误报告以查看实际错误吗?这将有助于调试。
  • var_dump($html) 在 php 7.1 上运行,结果类似于在 php 5.6 上的 echo $html。
  • 运行时没问题 follow stackoverflow.com/a/44131040/8916968 它的工作方式类似于在 php 5.6 上运行

标签: php


【解决方案1】:

file_get_html 更改 simple_html_dom 包含文件需要更改。 见下文,它对我有用。 见链接https://sourceforge.net/p/simplehtmldom/bugs/161/

从 PHP 7.1 开始可以解释负偏移量。 偏移量的默认值必须从-1 更改为0

function file_get_html($url, $use_include_path = false, $context=null, $offset = 0, $maxLen=-1, $lowercase = true, $forceTagsClosed=true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT)

【讨论】:

    【解决方案2】:

    我知道这有点旧,但你总是可以在这里下载最新版本 -> https://sourceforge.net/projects/simplehtmldom/

    截至本文的最新更新是 10-08-19

    【讨论】:

      【解决方案3】:

      我通过将方法“parse_slector()”(第 386 行)中的“simple_html_dom.php”文件更改为

      来避免这种情况
      $pattern = "/([\w\-:\*]*)(?:\#([\w\-]+)|\.([\w\-]+))?(?:\[@?(!?[\w\-]+)(?:([!*^$]?=)[\"']?(.*?)[\"']?)?\])?([\/, ]+)/is";
      

      在方法“read_tag()”中(第 722 行)

      if (!preg_match("/^[\w\-:]+$/", $tag)) {
      ...
      }
      

      诀窍是在模式上的“-”之前添加反斜杠

      【讨论】:

      • 这对我有用(PHP 7.3),非常感谢!我还删除了第 75 行 file_get_contents 中的偏移参数,并在第 70 行的 file_get_html 函数中将偏移设置为 0。
      猜你喜欢
      • 1970-01-01
      • 2012-01-17
      • 1970-01-01
      • 1970-01-01
      • 2015-02-14
      • 2016-07-30
      • 2013-03-23
      • 2014-01-15
      相关资源
      最近更新 更多