稲沢市よりお届けしてます。

Perlをはじめとしたプログラミング周りのあれこれについて。Perl界のモブキャラとして暗躍します(謎)。

Yahoo::Searchモジュールをjp仕様にして、YahooJapan検索順位を1000位まで順位チェックする

日本の検索と言えば、Yahoo。
ということで、Yahooでの検索結果を1000位まで調べるスクリプトを書いてみました。

◎ポイント
Yahoo::SearchモジュールのデフォルトYahoo検索APIは、米国版(.com)なので、それらを全て日本版(.jp)に変更すること。

◎具体的な手順
1.Yahoo::Searchモジュールのインストールフォルダを開き、「Searchフォルダ」と「Search.pm」をフォルダ内にコピペする(名前はSearch_jpにしました)

2.読み取り専用なので、書き込み可にして開く。

3.フォルダ構造はこんな感じ↓
Search_jp.pm
Search_jp

---Request.pm
--Response.pm
---Result.pm
---XML.pm

わかりづらい・・・orz

ので、画像でごまかす

4.気を取り直して、Search_jp.pm 内のAPI部分


# Normal web search
#
Doc =>
{
Url => 'http://search.yahooapis.com/WebSearchService/V1/webSearch',
ContextUrl => 'http://search.yahooapis.com/WebSearchService/V1/contextSearch',

ここの、yahooapis.com→yahooapis.jpに変更する。

5. 3で表示したpmたちのパッケージ名
Yahoo::Search→Yahoo::Search_jp に全て置き換える。

以上で下準備は終了。



で、Perlスクリプトはこれ↓


#!/usr/bin/perl -w
#
#Yahoo::Search_jpを使って特定URLの1000位までの順位をチェックする

use strict;
use Yahoo::Search_jp;
use encoding qw(shiftjis);


my $target_url="http://www.tbs.co.jp/cdtv/artistdb/artist3532.html";
my $target_words="AKB48";
my $language='ja'; #default=>undef
my $appid="あなたのYahooアプリケーションID";

my @Results = Yahoo::Search_jp->Results(Doc => $target_words,
AppId => $appid,
# The following args are optional.
# (Values shown are package defaults).
Mode => 'all', # all words
Start => 0,
Count => 100,
Type => 'any', # all types
AllowAdult => 1, # including porn
AllowSimilar => 0,
Language => $language,
);

for my $Result (@Results){
if ($target_url eq $Result->Url){
printf "Result: #%d\n", $Result->I + 1,
printf "Url:%s\n", $Result->Url;
printf "%s\n", $Result->ClickUrl;
printf "Summary: %s\n", $Result->Summary;
printf "Title: %s\n", $Result->Title;
printf "In Cache: %s\n", $Result->CacheUrl;
print "\n";
}else{
1;
}
}

print "100位まで調べました。\n";

my @Results2 = Yahoo::Search_jp->Results(Doc => $target_words,
AppId => $appid,
# The following args are optional.
# (Values shown are package defaults).
Mode => 'all', # all words
Start => 100,
Count => 100,
Type => 'any', # all types
AllowAdult => 1, # including porn
AllowSimilar => 0,
Language => $language,
);

for my $Result (@Results2){
if ($target_url eq $Result->Url){
printf "Result: #%d\n", $Result->I + 1,
printf "Url:%s\n", $Result->Url;
printf "%s\n", $Result->ClickUrl;
printf "Summary: %s\n", $Result->Summary;
printf "Title: %s\n", $Result->Title;
printf "In Cache: %s\n", $Result->CacheUrl;
print "\n";
}else{
1;
}
}

print "200位まで調べました。\n";

my @Results3 = Yahoo::Search_jp->Results(Doc => $target_words,
AppId => $appid,
# The following args are optional.
# (Values shown are package defaults).
Mode => 'all', # all words
Start => 200,
Count => 100,
Type => 'any', # all types
AllowAdult => 1, # including porn
AllowSimilar => 0,
Language => $language,
);

for my $Result (@Results3){
if ($target_url eq $Result->Url){
printf "Result: #%d\n", $Result->I + 1,
printf "Url:%s\n", $Result->Url;
printf "%s\n", $Result->ClickUrl;
printf "Summary: %s\n", $Result->Summary;
printf "Title: %s\n", $Result->Title;
printf "In Cache: %s\n", $Result->CacheUrl;
print "\n";
}else{
1;
}
}

print "300位まで調べました。\n";

my @Results4 = Yahoo::Search_jp->Results(Doc => $target_words,
AppId => $appid,
# The following args are optional.
# (Values shown are package defaults).
Mode => 'all', # all words
Start => 300,
Count => 100,
Type => 'any', # all types
AllowAdult => 1, # including porn
AllowSimilar => 0,
Language => $language,
);

for my $Result (@Results4){
if ($target_url eq $Result->Url){
printf "Result: #%d\n", $Result->I + 1,
printf "Url:%s\n", $Result->Url;
printf "%s\n", $Result->ClickUrl;
printf "Summary: %s\n", $Result->Summary;
printf "Title: %s\n", $Result->Title;
printf "In Cache: %s\n", $Result->CacheUrl;
print "\n";
}else{
1;
}
}

print "400位まで調べました。\n";

my @Results5 = Yahoo::Search_jp->Results(Doc => $target_words,
AppId => $appid,
# The following args are optional.
# (Values shown are package defaults).
Mode => 'all', # all words
Start => 400,
Count => 100,
Type => 'any', # all types
AllowAdult => 1, # including porn
AllowSimilar => 0,
Language => $language,
);

for my $Result (@Results5){
if ($target_url eq $Result->Url){
printf "Result: #%d\n", $Result->I + 1,
printf "Url:%s\n", $Result->Url;
printf "%s\n", $Result->ClickUrl;
printf "Summary: %s\n", $Result->Summary;
printf "Title: %s\n", $Result->Title;
printf "In Cache: %s\n", $Result->CacheUrl;
print "\n";
}else{
1;
}
}

print "500位まで調べました。\n";

my @Results6 = Yahoo::Search_jp->Results(Doc => $target_words,
AppId => $appid,
# The following args are optional.
# (Values shown are package defaults).
Mode => 'all', # all words
Start => 500,
Count => 100,
Type => 'any', # all types
AllowAdult => 1, # including porn
AllowSimilar => 0,
Language => $language,
);

for my $Result (@Results6){
if ($target_url eq $Result->Url){
printf "Result: #%d\n", $Result->I + 1,
printf "Url:%s\n", $Result->Url;
printf "%s\n", $Result->ClickUrl;
printf "Summary: %s\n", $Result->Summary;
printf "Title: %s\n", $Result->Title;
printf "In Cache: %s\n", $Result->CacheUrl;
print "\n";
}else{
1;
}
}

print "600位まで調べました。\n";

my @Results7 = Yahoo::Search_jp->Results(Doc => $target_words,
AppId => $appid,
# The following args are optional.
# (Values shown are package defaults).
Mode => 'all', # all words
Start => 600,
Count => 100,
Type => 'any', # all types
AllowAdult => 1, # including porn
AllowSimilar => 0,
Language => $language,
);

for my $Result (@Results7){
if ($target_url eq $Result->Url){
printf "Result: #%d\n", $Result->I + 1,
printf "Url:%s\n", $Result->Url;
printf "%s\n", $Result->ClickUrl;
printf "Summary: %s\n", $Result->Summary;
printf "Title: %s\n", $Result->Title;
printf "In Cache: %s\n", $Result->CacheUrl;
print "\n";
}else{
1;
}
}

print "700位まで調べました。\n";

my @Results8 = Yahoo::Search_jp->Results(Doc => $target_words,
AppId => $appid,
# The following args are optional.
# (Values shown are package defaults).
Mode => 'all', # all words
Start => 700,
Count => 100,
Type => 'any', # all types
AllowAdult => 1, # including porn
AllowSimilar => 0,
Language => $language,
);

for my $Result (@Results8){
if ($target_url eq $Result->Url){
printf "Result: #%d\n", $Result->I + 1,
printf "Url:%s\n", $Result->Url;
printf "%s\n", $Result->ClickUrl;
printf "Summary: %s\n", $Result->Summary;
printf "Title: %s\n", $Result->Title;
printf "In Cache: %s\n", $Result->CacheUrl;
print "\n";
}else{
1;
}
}

print "800位まで調べました。\n";

my @Results9 = Yahoo::Search_jp->Results(Doc => $target_words,
AppId => $appid,
# The following args are optional.
# (Values shown are package defaults).
Mode => 'all', # all words
Start => 800,
Count => 100,
Type => 'any', # all types
AllowAdult => 1, # including porn
AllowSimilar => 0,
Language => $language,
);

for my $Result (@Results9){
if ($target_url eq $Result->Url){
printf "Result: #%d\n", $Result->I + 1,
printf "Url:%s\n", $Result->Url;
printf "%s\n", $Result->ClickUrl;
printf "Summary: %s\n", $Result->Summary;
printf "Title: %s\n", $Result->Title;
printf "In Cache: %s\n", $Result->CacheUrl;
print "\n";
}else{
1;
}
}

print "900位まで調べました。\n";

my @Results10 = Yahoo::Search_jp->Results(Doc => $target_words,
AppId => $appid,
# The following args are optional.
# (Values shown are package defaults).
Mode => 'all', # all words
Start => 900,
Count => 100,
Type => 'any', # all types
AllowAdult => 1, # including porn
AllowSimilar => 0,
Language => $language,
);

for my $Result (@Results10){
if ($target_url eq $Result->Url){
printf "Result: #%d\n", $Result->I + 1,
printf "Url:%s\n", $Result->Url;
printf "%s\n", $Result->ClickUrl;
printf "Summary: %s\n", $Result->Summary;
printf "Title: %s\n", $Result->Title;
printf "In Cache: %s\n", $Result->CacheUrl;
print "\n";
}else{
1;
}
}

print "1000位まで調べました。\n";

で、上の実行結果がこれ


【課題】
(1)なんか、冗長。もう少しエレガントに書けないもんかな〜?

(2)今は、URL完全一致のみ検索なので、下層ページもチェックできるようにする。(たぶんスグできる)
【(2)解決】→普通にマッチングで解決しました。