キーワードとURLを元に、GoogleとYahoo!検索の結果順位を表示するツールを作ってみた。
はじめはrubyでやろうと思ったけど、事前調査の段階でPHPの方がありものの組み合わせでチャッチャとできそうだったのでPHPでやることにした。
はじめはrubyでやろうと思ったけど、事前調査の段階でPHPの方がありものの組み合わせでチャッチャとできそうだったのでPHPでやることにした。
PHP5+zendframework+Yahoo!APIという感じ。
google検索は、スクレイピングで対応した。
スクレイピングには、手元にあった「SPIDERING HACKS」に付属していたソースを流用した。
で、作ったのがこれ。PHPのお作法とかあんまよくわからないし、改善の余地もいろいろあると思うので、ご指摘いただけるとうれしいです。
/controller/SearchController.php
<?phpscrape_func.phpがSPIDERING HACKSのソース。
require_once 'Zend/Controller/Action.php';
class SearchController extends Zend_Controller_Action {
public function indexAction(){
require_once ('Zend/Http/Client.php');
require_once dirname(__FILE__).'\..\models\common\html\scrape_func.php';
$keyword = $this->_getParam("keyword");
$url = $this->_getParam("url");
if(strlen($keyword) > 0 && strlen($url) > 0) {
$googleRank = $this->getGoogleRank($keyword, $url);
$yahooRank = $this->getYahooRank($keyword, $url);
$this->view->assign('googleRank', $googleRank);
$this->view->assign('yahooRank', $yahooRank);
$this->view->assign('keyword', $keyword);
$this->view->assign('url', $url);
}
}
private function getGoogleRank($keyword, $url){
$client = new Zend_Http_Client("http://www.google.co.jp/search");
$client->setConfig(array('maxredirects' => 0, 'timeout' => 30));
$client->setParameterGet(array('q' => $keyword,'ie' => 'utf-8','hl' => 'ja','num' => '100','start' => 0));
// リクエストの送信
try {
$response = $client->request('GET');
} catch (Zend_Exception $exception) {
echo "エラーメッセージ: " . $exception->getMessage();
}
// ヘッダー&ボディの取得。ヘッダは放置。
if ($response != null) {
$body = $response->getBody();
}
$body = cleanString($body);
$_rawData = getBlock("<li><div class=g>", "</ol></div>", $body, true);
$_rawData = explode("<li>", $_rawData);
// <li>でexplodeすると、配列の最初の要素に空が入るのでとっぱらう。
array_shift($_rawData);
$i = 0;
$isFound = false;
foreach( $_rawData as $_rawBlock ) {
$_rawBlock = trim( $_rawBlock );
// インデント表示される検索結果は飛ばすことにする。
if(strstr($_rawBlock, "<div class=g style=")) {
continue;
}
$i++;
if( strlen( $_rawBlock ) > 0 ) {
//検索結果のサイトURL部分の抽出
$url = getElement("span", $_rawBlock);
$url = substr($url, 0, strpos($url, "/", 0));
}
if($url == $url) {
$isFound = true;
break;
}
}
if(!$isFound){
$i = null;
}
return $i;
}
private function getYahooRank($keyword, $url){
$appid = "xxxxxxxxxxxx";
$client = new Zend_Http_Client("http://search.yahooapis.jp/WebSearchService/V1/webSearch");
$client->setConfig(array('maxredirects' => 0, 'timeout' => 30));
$isFound = false;
$rank = 0;
//100位まで探す仕様かつ、APIのMAXが50件表示なので最大2週する
for($i = 0; $i < 2; $i++){
$start = 50 * $i + 1;
$client->setParameterGet(array('appid' => $appid, 'query' => $keyword,'format' => 'html', 'results' => '50', 'start' => $start));
// リクエストの送信
try {
$response = $client->request('GET');
} catch (Zend_Exception $exception) {
echo "エラーメッセージ: " . $exception->getMessage();
}
// ヘッダー&ボディの取得。
if ($response != null) {
$body = $response->getBody();
}
$xml = simplexml_load_string($body);
foreach ($xml->Result as $result) {
$rank++;
if( strlen($result->Url) > 0 ) {
$resultUrl = str_replace("http://", "", $result->Url);
$resultUrl = substr($resultUrl, 0, strpos($resultUrl, "/", 0));
if($resultUrl == $url) {
$isFound = true;
break;
}
}
}
}
if(!$isFound){
$rank = null;
}
return $rank;
}
}
view/script/search/index.phtml
<html>追記(8/22):
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>google・yahoo順位チェッカー</title>
</head>
<body>
<h1>google・yahoo順位チェッカー</h1>
<p>
調べたいサイトのURLとキーワードを記入してください。100位まで検索します。
</p>
<form action="/search">
URL: <input type="text" name="url" value="<?php if($this->url) {echo $this->url;}?>"> キーワード: <input type="text" name="keyword" value="<?php if($this->keyword) {echo $this->keyword;}?>"> <input type="submit" value="check"><br />
<div style="color:red;">http://以降を入力し、最後のスラッシュは入れないでください。<br />
例:http://www.google.com/ → www.google.com</div>
</form>
<h2><?php if($this->googleRank != null)echo "Google:順位は<em style=\"color:red\">".$this->googleRank."</em>位です。<br />"; else if($this->keyword) echo "Google:100位以内に入っていません。<br />"; ?></h2>
<h2><?php if($this->yahooRank != null)echo "Yahoo!:順位は<em style=\"color:red\">".$this->yahooRank."</em>位です。<br />"; else if($this->keyword) echo "Yahoo!:100位以内に入っていません。<br />"; ?></h2>
</body>
</html>
googleの方がさっそく動かなくなった。抜き出しのタグ指定の部分は、何パターン化に分かれるようなので、それなりの期間のテストをしてパターンを洗い出す必要がありそう。
コメントする