Simple_Html_Dom,PHPQuery之外的新选择

PHPQuery是一个PHP的Jquery实现,对于熟悉jQuery的人来说,可以非常方便的机进行网页解析。可是对畸形怪状的网页和编码支持不是太好,虽然有各种hack方式,但是始终很麻烦,而且从2009年以后就不怎么更新了。于是找替代方案,找到了这个Simple_Html_Dom,文档也很详细,立刻用这奇形怪状的网页进行了测试。[php]<?php
//演示
header("Content-type: text/html; charset=utf-8");
set_time_limit(0);
include ("simple_html_dom.php");
$url = 'http://ihipop.info';
$html = file_get_html($url);
echo $html -> find('title', 0) -> plaintext;
$x = $html -> find('a[rel=bookmark]');

foreach($x as $key => $value) {
echo '<br />';
echo $value -> plaintext;
}

$html -> clear();
//内存释放
?>[/php]
[php]<?php
//演示
header("Content-type: text/html; charset=euc-jp");
set_time_limit(0);
include ("simple_html_dom.php");
$url = 'http://storeuser11.auctions.yahoo.co.jp/jp/user/makototakahashi0316?alocale=0jp&mode=0&u=makototakahashi0316';
$html = file_get_html($url);

$trs = $html -> find('#list01 table tr');

foreach($trs as $key => $tr) {
if($key == 0) {
continue ;
}
$line = $tr -> find('td', 0) -> find('a', 0);
$title = $line -> plaintext;
if($title) {
$url = $line -> href;
echo $title . '---->' . $url . '<br />';
}

}
?>[/php]

相对来讲使用没有phpQuery方便,支持的选择器也少,比如一些:first :not的CSS选择器就不支持
但是他输入是什么编码 输出也是什么编码,因而不容易出现乱码,可以作为使用phpQuery后没办法的替代品

Author Info :
  • From:Simple_Html_Dom,PHPQuery之外的新选择
  • URL:https://blog.ihipop.com/2011/08/2671.html
  • Please Reserve This Link,Thanks!
  • 发表回复

    您的电子邮箱地址不会被公开。 必填项已用*标注