正则规则写好后,页面一旦有改变就要重新修改正则。
先提取页面的 DOM,有没有比较好的办法?
正则规则写好后,页面一旦有改变就要重新修改正则。
先提取页面的 DOM,有没有比较好的办法?
我想你需要的是 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 的能力远远超乎你的想象 ... 有空读读手册不是坏事恩 ...
你说的很对。我现在就用xpath。。。
立即学习“PHP免费学习笔记(深入)”;
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号