【发布时间】: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 ;
}
}
但它不起作用,我知道<p> 标记,我尝试为此添加 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->find('title',0)->plaintext;)时,代码可以工作,但是对于具有多个类的类,它会失败。 -
首先,想想你在命名什么以及为什么。如果 getdomain 是一种方法,还要考虑
camelCase,我很确定如果该方法/函数不需要其他参数,则方法或函数总是需要()- 如果它是一个已定义的常量,那么我会使用CAPSCASE,不是因为你想对其他开发者大喊大叫,而是提醒他们getdomain是一个常数。否则它看起来像是一个错字,要么是一个开头缺少$的变量,要么是一个缺少()的函数/方法 - 最后getdomain听起来像getter那么如何设置它?跨度>
标签: php html html-parsing