外部テキストファイルから読み込み、一気にチェックできるようにしました。---Yahoo::Searchモジュールをjp仕様にして、YahooJapan検索順位を1000位まで順位チェックする
この作り方が正しいかどうかはわかりませんが、
僕はPerlを書くとき、
1.全体のプログラムをイメージし、条件分岐とかで区切って細かくパーツに分ける
2.細かいパーツを一つ一つイメージ通りに動くかどうかチェックしながら仕上げる
3.できたパーツを組み合わせて、プログラム全体を組み立てる
4.使いながら、「もっとこうしたい」を形にしていく
という手順を踏んでいます。もちろん四苦八苦しながら(汗)
さて、
今回いじったYahoo_Japan_1000search.plもバージョン3まできました。
使ってて思ったんですが、
いちいち、url とkeywords をハードコーディングするのがメンドくさ過ぎる。
ので、
外部テキストファイルを一気に読み込む形にしました。
#!/usr/bin/perl -w
#
#Yahoo::Search_jpを使って特定URLの1000位までの順位をチェックするuse strict;
use Yahoo::Search_jp;
use encoding qw(shiftjis);open CHECK, " < check.txt";
my @data;
my @attrs = qw(url keywords);
while(){
chomp;
my %check_data;
@check_data{@attrs} = split/\t/;
my $ref_check_data = \%check_data;
push @data, $ref_check_data;
}foreach (@data){
my $target_url = $_ -> {url};
my $target_words = $_ -> {keywords};
my $language='ja'; #default=>undef
my $appid="あなたのYahooアプリケーションID";print $target_url ." のキーワード「" .$target_words ."」で調べます\n\n";
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 ( $Result->Url =~ /$target_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 ( $Result->Url =~ /$target_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 ( $Result->Url =~ /$target_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 ( $Result->Url =~ /$target_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 ( $Result->Url =~ /$target_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 ( $Result->Url =~ /$target_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 ( $Result->Url =~ /$target_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 ( $Result->Url =~ /$target_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 ( $Result->Url =~ /$target_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 ( $Result->Url =~ /$target_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\n";
}
個人的には、覚えたてのリファレンスを使ったあたりが好き。
タブ区切りの入力用テキストファイルから
にデータを格納し、ハッシュのリファレンスを
%check_data = {
"url" => 'http://www.hogehoge.com/',
"keywords" => 'ほげ',
};
配列@dataに入れていくあたり。
自分のPerl慣れを感じた。
Dan Kogai氏の言ってた
perl - 現代的な Perl を再習得する方法は
は今の僕なら激しく同意。
これからも勇猛果敢に晒してゆきます。