指定したTwitterアカウントの全ツイートを取得(perl版)

Twitterアカウントを指定して、全ての発言(ツイート)を取得するための perl スクリプトです。

目的は、

  • あなたの彼の浮気調査

でもいいし(笑)、真面目に解析してもいいし。私の場合は、先に書いた「絡ったー」がらみで使う予定です。

twitter api を使うとアクセス制限(1時間に500回ぐらいかな)となるので、公式サイト http://twitter.com/ から直接引っ張ってきます。なので、公式サイトの形式が変わると取れなくなるんですが、ま、ひとまず、こんな感じで取得できるとという例として。

インストールは、

  • activeperl などをダウンロード
  • wget をダウンロード
    分かる人は、cUrl に変更しても ok

コマンドラインから

perl krmall.pl [アカウント]

とすると、

  • 全発言のファイル アカウント.txt
  • 絡む人のファイル アカウント_st.txt

が作成されます。これをメモ帳で見る、ぐらいですね。HTML形式に変換し直すと、リンク先に飛べたりして便利です。

例えば、孫正義さんの全ツイートを取得する場合は、

perl krmall.pl masason

とします。

それなりに時間がかかりますが、WEBサイト等でクリックしていくよりも便利でしょう。

え~と、どういう風に取得しているかというと、

  1. 指定したアカウントのツイート数を取得
    http://twitter.com/アカウント
  2. 全ページ数 = ツイート数/20 + 1 を計算。
  3. ページ毎に過去を取得
    http://twitter.com/アカウント?page=n
  4. IDやツイートなどを検索して取り出し。

な感じです。いわゆるサイト経由のクローリングと同じです。

以下は、ソースコード。

# 指定アカウントの全発言を取得
$user = $ARGV[0]; # アカウント
$wget = "wget"; if ( $user eq "" ) {
print "perl krmall.pl [アカウント]";
exit;
} # 現在のツイート数を取得
`$wget http://twitter.com/$user -Otemp.txt`;
open( FILE, &quot;<temp.txt&quot; );
while(<FILE>) {
if ( /<span id=&quot;update_count&quot; class=&quot;stat_count&quot;>([0-9,]+)<\/span>/ ) {
$cnt = $1;
$cnt =~ s/,//;
break;
}
}
close( FILE ); $pmax = int($cnt/20)+1;
print &quot;account: $user count: $cnt pages: $pmax\n&quot;;  # 指定アカウントを全て読み込み
unlink( &quot;$user.txt&quot; );
open( OUT, &quot;>>$user.txt&quot; ); for ($i=1; $i<=$pmax; $i++ ) {
`$wget http://twitter.com/$user?page=$i  -Otemp.txt`;

open( FILE, &quot;<temp.txt&quot; );
while(<FILE>) {
if ( /<span class=&quot;entry-content&quot;>/ ) {
$text = $_;
if ( !/<\/span>/ ) {
while(<FILE>) {
if ( /<\/span>/ ) {
$text .= $_;
last;
}
$text .= $_;
}
}
$text =~ s/\n//g;
$text =~ s/\r//g;
$text =~ /<span class=&quot;entry-content&quot;>(.*)<\/span>/;
$text = $1;

<FILE>; <FILE>;
$id = <FILE>;
$id =~ /status\/([0-9]+)/;
$id = $1;
$date = <FILE>;
$date =~ /data=&quot;{time:'([^']+)'}/;  #&quot;
$date = $1;

$text =~ s/<[^>]+>//g;

print OUT &quot;---\n&quot;;
print OUT &quot;$id\n&quot;;
print OUT &quot;$text\n&quot;;
print OUT &quot;$date\n\n&quot;;
}
}
close( FILE );
}
close( OUT ); # &quot;
# 統計表示
open( OUT, &quot;<$user.txt&quot;);
open( ST,  &quot;>${user}_st.txt&quot;);
while(<OUT>) {
if ( /^---/ ) {
$_ = <OUT>; chomp; $id = $_;
$_ = <OUT>; chomp; $text = $_;
$_ = <OUT>; chomp; $date = $_;

$_ = $text ;
@res = /(@[A-Za-z0-9_]+)/g;
foreach $re ( @res ) {
$users{ $re } = $users{ $re } + 1;
}
}
}
close( OUT );

foreach $re ( sort {$users{$b} <=> $users{$a}} keys %users ) {
if ( $re ne &quot;@&quot;.$user ) {
print ST &quot;$re (&quot;.$users{$re}.&quot;)\n&quot;;
}
}
close( ST );


2012/01/11 追記

上記のコードでは最新の公式クライアントでは動かないので、下記を試してみてください。C# で記述しています。

指定したTwitterアカウントの全ツイートを取得(暫定.NET版) | Moonmile Solutions Blog
http://www.moonmile.net/blog/archives/2851

カテゴリー: 開発 パーマリンク

指定したTwitterアカウントの全ツイートを取得(perl版) への2件のフィードバック

  1. kino のコメント:

    はじめまして。
    このたび、検索からこちらのサイトを発見してお伺いさせていただきました。
    ツイートを取得する件で質問なのですが、「wget」さんはどのようにダウンロードすることができるのでしょうか?
    色々と検索したのですが、こちらのサイトがちょっと英語でわかりにくくダウンロード先がよくわからなく・・・。
    よろしくご指導いただければと思います。

  2. masuda のコメント:

    「wget windows ダウンロード」で検索して、
    wget(Windows版) – 俺の基地
    http://yakinikunotare.boo.jp/orebase/index.php?wget%A1%CAWindows%C8%C7%A1%CB
    とか。

コメントは停止中です。