开发者社区> 问答> 正文

php 抓取的页面如何处理可以只保留DOM结构,去掉CSS和JS?

正则规则写好后,页面一旦有改变就要重新修改正则。
先提取页面的 DOM,有没有比较好的办法?

展开
收起
a123456678 2016-03-25 09:48:14 2466 0
2 条回答
写回答
取消 提交回答

  • Hello World


    Hey Welcome
    2019-07-17 19:13:31
    赞同 展开评论 打赏
  • 我想你需要的是 php 的 DOM 模块 ... 默认有安装不用担心 ...

    因为不知道你的实际应用场景是什么 ... 给你写个简单的例子吧 ...

    <?php
    /* i heard that you need DOM ..? */
    $doc = new DOMDocument();
    
    /* i wrote a simple page ... change it to a curl result ... */
    $doc->loadHTML( <<<HTML_SECTION
    <html><head><title>Sunyanzi's Test</title></head>
    <body>
      <h1>Hello World</h1>
      <a href="http://segmentfault.com/" id="onlylink">Hey Welcome</a>
    </body></html>
    HTML_SECTION
    );
    
    /* now we should try to get something ... */
    $h1Elements = $doc->getElementsByTagName( 'h1' );
    
    /* this line prints "Hello World" ... */
    foreach( $h1Elements as $h1Node ) 
        echo $h1Node->nodeValue, PHP_EOL;
    
    /* and this line prints "http://segmentfault.com/" ... */
    echo $doc->getElementById( 'onlylink' )->getAttribute( 'href' ), PHP_EOL;
    
    /* now i will introduce something advanced ... using XPath ... */
    $xpath = new DOMXPath( $doc );
    
    /* also prints "http://segmentfault.com/" ... locate via h1 ... */
    echo $xpath->evaluate(
        'string(//h1[text()="Hello World"]/following-sibling::a/@href)'
        ), PHP_EOL;

    基本上 ... 等到你熟练掌握 XPath 之后 ... 你会发现 DOM 比正则要灵活得多 ...

    php 处理 XML 的能力远远超乎你的想象 ... 有空读读手册不是坏事恩 ...

    2019-07-17 19:13:31
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
JavaScript面向对象的程序设计 立即下载
Delivering Javascript to World 立即下载
编程语言如何演化-以JS的private为例 立即下载