【问题标题】:PHP Simple HTML DOM Parser returns null on multiple classesPHP Simple HTML DOM Parser 在多个类上返回 null
【发布时间】:2018-07-10 15:45:31
【问题描述】:

我正在尝试使用 Simple HTML DOM Parser 从远程 url 解析这个 HTML:

<div class="_5pbx userContent _3ds9 _3576" data-ft="data-ft='{"tn":"K"}'">
<p>text to be parsed</p>
<p>the rest of text</p>
</div>

我使用的PHP狙击如下:

include (getdomain . "/lib/simple_html_dom.php");

$html = new simple_html_dom();
$get = file_get_contents("http://localhost/get/d.json");
$html->load($get);
foreach ($html->find('div[class=_5pbx userContent _3ds9 _3576]') as $link) {
 if(isset($link)){
          echo $link->plaintext ;
    }
  }

但它不起作用,我知道&lt;p&gt; 标记,我尝试为此添加 if 语句但仍然不起作用,如下所示:

include (getdomain . "/lib/simple_html_dom.php");

$html = new simple_html_dom();
$get = file_get_contents("http://localhost/get/d.html");
$html->load($get);
foreach ($html->find('div[class=_5pbx userContent _3ds9 _3576]') as $link) {
 if(isset($link)){
foreach($link->find('p') as $tag)
    {
          echo $tag->plaintext ;
    }
  }
}

但一切都没有奏效:(

有什么想法吗?

【问题讨论】:

  • 您是否收到任何显示或记录的 PHP 错误或警告?另外,在您的包含中,getdomain 是您系统中定义的符号吗?还是一个函数?
  • Yes getdomain 来获取域,当我尝试获取其他元素(如标题echo $html-&gt;find('title',0)-&gt;plaintext;)时,代码可以工作,但是对于具有多个类的类,它会失败。
  • 首先,想想你在命名什么以及为什么。如果 getdomain 是一种方法,还要考虑camelCase,我很确定如果该方法/函数不需要其他参数,则方法或函数总是需要() - 如果它是一个已定义的常量,那么我会使用CAPSCASE ,不是因为你想对其他开发者大喊大叫,而是提醒他们getdomain 是一个常数。否则它看起来像是一个错字,要么是一个开头缺少 $ 的变量,要么是一个缺少 () 的函数/方法 - 最后 getdomain 听起来像 getter 那么如何设置它?跨度>

标签: php html html-parsing


【解决方案1】:

我试过这个。它正在工作..

<?php
include (getcwd() . "/simple_html_dom.php");
$html = new simple_html_dom();
$get = file_get_contents("http://localhost/zdemo/php%20selenium/d.json");
$html->load($get);
foreach ($html->find('div[class=_5pbx userContent _3ds9 _3576]') as $link) 
{
    if(isset($link))
    {
          echo $link->plaintext ;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-23
    • 1970-01-01
    • 2015-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    相关资源
    最近更新 更多