duanbeng1923 2017-03-14 10:19
浏览 59
已采纳

simple_html_dom访问div里面的ul

I'm using simple_html_dom for class html grabbing script, i got a problem when trying to grab ul inside a div

HTML

<div class="attributes">
      <div class="headline">test header</div>
                <ul>
                  <li>test 1</li>
                  <li>test 2</li>
                  <li>test 3</li>

                </ul>
    </div>

PHP

//call to function
$url = 'http://example.com';

$data = dlPage2($url,'.attributes');
echo $data;


//function

function dlPage2($href,$element) {

    $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, $href);
    curl_setopt($curl, CURLOPT_REFERER, $href);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.125 Safari/533.4");
    $str = curl_exec($curl);
    curl_close($curl);

    // Create a DOM object
    $dom = new simple_html_dom();
    // Load HTML from a string
    $dom->load($str);
$dom= $dom->find($element,0)->outertext;
    return $dom;
    }

above code i can grab whole <div class="attributes"> but i need to get html of <ul> tag inside that div,

can someone help me to change this please

  • 写回答

1条回答 默认 最新

  • douzi0609 2017-03-14 10:33
    关注

    You have to select <ul> inside $element by using

    $dom = $dom->find($element.' ul', 0)->outertext;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)