phpのSnoopy+htmlSQLでスクレイピングする時、POSTメソッドでhttpリクエスト出来ない対処方法。
phpのSnoopyライブラリはPEAR::HTTP_Requestと同じようなライブラリなのですが、HTTPリクエスト後、スクレイピングする処理は様々な方法があるようです。
http://saboten009.blogspot.com/2008/08/php.html
僕は使用した事が無いのですが、Tidy関数を利用すると上手くスクレイピング出来るという記事を良く目にするのですが、導入の仕方がどうもわからなくて他の方法を探していました。
色々検索すると、Snoopy+htmlSQLでもスクレイピング出来て、
require_once("Snoopy.class.php"); require_once("htmlsql.class.php");
だけで使用出来るので、実際に使ってみました。
下記の本家のサイトにデモがあります。
http://www.jonasjohn.de/lab/htmlsql.htm
SQLを書くようにHTMLをスクレイピング出来るので面白いなーと思っていたのですが、HTTPリクエストのPOSTメソッドが利用出来ないという事に気づきました。(Snoopy単体だと出来るんですけどね…)
POSTでHTTPリクエストしたかったので、自分でhtmlsql.class.phpを修正しました。
htmlsql.class.phpの$varsプロパティの追加
var $vars = '';//90行目辺りに追加
htmlsql.class.phpのconnectメソッドの修正
function connect($type, $resource, $method='get'){ if ($type == 'url'){ if($method == 'post'){ return $this->_fetch_url($resource, 'post'); } else { return $this->_fetch_url($resource, 'get'); } } else if ($type == 'file') { if (!file_exists($resource)){ $this->error = 'The given file "'.$resource.' does not exist!'; return false; } $this->page = file_get_contents($resource); return true; } else if ($type == 'string') { $this->page = $resource; return true; } return false; }
htmlsql.class.phpの_fetch_urlメソッドの修正
function _fetch_url($url, $method){ $parsed_url = parse_url($url); if (!isset($parsed_url['scheme']) or $parsed_url['scheme'] != 'http'){ $this->error = 'Unsupported URL sheme given, please just use "HTTP".'; return false; } if (!isset($parsed_url['host']) or $parsed_url['host'] == ''){ $this->error = 'Invalid URL given!'; return false; } $host = $parsed_url['host']; $host .= (isset($parsed_url['port']) and !empty($parsed_url['port'])) ? ':'.$parsed_url['port'] : ''; $path = (isset($parsed_url['path']) and !empty($parsed_url['path'])) ? $parsed_url['path'] : '/'; $path .= (isset($parsed_url['query']) and !empty($parsed_url['query'])) ? '?'.$parsed_url['query'] : ''; $url = 'http://' . $host . $path; $this->init_snoopy(); if($this->snoopy->fetch($url) && $method == 'get'){ $this->page = $this->snoopy->results; // empty buffer: $this->snoopy->results = ''; } else if($this->snoopy->submit($url, $this->vars) && $method == 'post'){ $this->page = $this->snoopy->results; // empty buffer: $this->snoopy->results = ''; } else { $this->error = 'Could not establish a connection to the given URL!'; return false; } return true; }
下記が呼出しの例です。
$wsql = new htmlsql(); $wsql->vars['page'] = $i;//POSTしたいデータの例 $wsql->connect('url', 'http://d.hatena.ne.jp/camelmasa/', 'post');//第3引数に"post"と書けばPOSTメソッドで通信します。
htmlsqlを使用する人はあまりいないかあ。
こうゆう事ってperlの方が得意そう。
最近のスクレイピング技術ってどうやってるんだろう?
Xpath?
[PR]Spreeの情報を集めています。
ECを持ちたい方、仕事でECを使いたい方向けのコミュニティサイトです。
このサイトでは世界で最も使用されているECの1つであるSpreeについての情報を提供しています。
http://spreecommerce.jp/