【问题标题】:PHP Simple HTML DOM object decodingPHP 简单 HTML DOM 对象解码
【发布时间】:2023-03-27 16:05:02
【问题描述】:

我尝试这样做:

$html = file_get_html ( 'http://www.ebay.com/cln/explorer/_ajax?page=1&ipp=16&catids=37958' );
foreach ( $html->find ( 'div[class="connection"]' ) as $collection ) {
    echo "found collections: ".count($collection);

问题是,AJAX 请求返回的文件包含如下编码的元素:

<div class=\"collection\" data-collectionid=\"75336256016\">
<div class=\"header\">

谁能帮我将 DOM 对象中的所有\" 转换回正常的"。 或更改-&gt;find 命令以找到正确的元素。

非常感谢!

【问题讨论】:

  • 那个页面给你 JSON 加上最后的 html 评论。

标签: php ajax parsing dom


【解决方案1】:
$html = file_get_html ( 'http://www.ebay.com/cln/explorer/_ajax?page=1&ipp=16&catids=37958' );
$html = stripslashes($html);

var_dump($html);

string '<div class="collection" data-collectionid="75336256016">
<div class="header">' (length=78)

【讨论】:

  • 消除 \'s 的魅力。但是, $html 不再是对象。不能使用 $html->find 就可以了。有什么想法吗?
  • 好吧,我不知道你在 file_get_html 函数中做了什么。您应该在获取 url 的内容后立即使用stripslashes,然后将其转换为 DOM 对象。
  • 我在simplehtmldom.sourceforge.net/manual.htm 关注手册。 $html = file_get_html('google.com/');很抱歉打扰您,但您能帮我完成您刚才描述的步骤吗?
  • 已解决。感谢您的帮助!
【解决方案2】:

ebay 的响应是 JSON,但在响应的末尾有一个&lt;!-- RlogId t6%60jjpfg%3C%3D%60mb6a54d.47e3-143fd4a3ae7-0x32337a --&gt; 或类似的东西。

首先你需要清理那个字符串..

比json_decode .. html键的值就是你需要解析的html..

一旦您清理并 json_decode 响应,您寻找的真实 html 将位于名为 html 的对象上。看看下面的代码。我很确定您可以使用更好的 var 名称...

include('simplehtmldom/simple_html_dom.php');
$html = file_get_html ( 'http://www.ebay.com/cln/explorer/_ajax?page=1&ipp=16&catids=37958' );
$str = $html->save();


$strparts = explode('<!-- RlogId',$str);
$json = $strparts[0];


$htmlcleanedup = json_decode($json);

$domhtml = str_get_html($htmlcleanedup->html);

$ret = $domhtml->find('div[class=collection]'); 

echo count($ret);
?>

【讨论】:

  • 当然.. 但是如果 Ebay 把那个愚蠢的随机代码放在这个响应下面,这意味着他们不想让 ppl 使用它..
【解决方案3】:

只需使用stripslashes()函数:

http://php.net/stripslashes

【讨论】:

  • 谢谢,不知道这个功能!
猜你喜欢
  • 1970-01-01
  • 2016-07-30
  • 2012-01-17
  • 1970-01-01
  • 1970-01-01
  • 2014-01-15
  • 2011-08-27
  • 2018-03-05
相关资源
最近更新 更多