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


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

php css JavaScript

幸せD魔法 9 years, 10 months ago

我想你需要的是 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 的能力远远超乎你的想象 ... 有空读读手册不是坏事恩 ...

Mr.HC answered 9 years, 10 months ago

Your Answer