duanmu5641 2014-02-04 14:06
浏览 124
已采纳

PHP简单的HTML DOM对象解码

I try to do this:

$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);

Problem is, the returned file from the AJAX request contains elements encoded like:

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

Can anyone please help me to transform all the \" in the DOM object back to the normal ". Or change the ->find command to find the right element.

Thanks so much!

  • 写回答

3条回答 默认 最新

  • dongxie2613 2014-02-04 14:36
    关注

    The response of the ebay is JSON, but there is a <!-- RlogId t6%60jjpfg%3C%3D%60mb6a54d.47e3-143fd4a3ae7-0x32337a --> or something like this at the end of the response.

    First you need to clean up that string..

    Than json_decode .. value of the html key is your html that you need to parse..

    Once you cleaned up and json_decode the response the real html you looked for will be on the object named html.. take a look the code below.. I am pretty sure you can use better var names...

    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);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?