Nacky - Snowland.net

Nacky(Issei Ishii)がDJ/Composerのようなふりして書き散らすblogサイト

Info

Nacky - Snowland.net - blogを移転しました

2011-06-16

PHP+tidy+XMLでスクレイピング

他のサイトの情報がほしーい
マッシュアップしたーい

みたいな感じで他サイトのデータが欲しいとき.

・環境
CentOS5 + Apache2 + PHP5.1
・必要なもの
php-tidy
pear HTTP/Request2

// ページ取得
require_once "HTTP/Request2.php";
$req = new HTTP_Request2('取得先URL');
$res = $req->send();
$html = $res->getBody();

// tidyで整形
$tidy = new tidy;
$tidy->parseString($html, array('indent'=>true,'output-xml'=>true), 'utf8');
$tidy->cleanRepair();

// xmlとして解析
$xml = new SimpleXMLElement($tidy);
$dat = $xml->xpath('欲しいデータにアクセス');
ということで簡単だったわ!20分ぐらいでできちゃった.
ただ実際には$tidy->cleanRepair()しても の処理がなんかおかしかったので,途中で を削除する処理を入れました.

posted at 19:31:59 on 2011-06-16 by nacky - Category: Com/Tech ( PHP )

ソニーストア

TrackBack

Comments

No comments yet