切り抜き詳細
- 発行日時
- 2008-10-1 1:46
- 見出し
- 外部アドレスからのテキスト抽出
- 記事詳細
- signal様、tezcello様、以前はありがとうございました。 signal様の作成していただけたスクリプトを埋め込み型に改造して使用しています。 次は、別ページの取得に挑戦してみましたが、どうしても失敗してしまうので、質問させていただきます。 signal様のスクリプトを見ながら作成してみたんですが、エラーメッセージは出ず、ただスペース及び余白としてだけでてしまいます、やり方はあってるはずなんですけど・・・。 今度は、http://tenvi.nexon.co.jp/community/free.aspを取得しようとおもい改造してみましたが・・・。 <?php $datadir = ' com ' ; # 更新日ファイルが存在しないとき if ( ! file_exists ( " {$datadir} /update " ) ) { touch ( " {$datadir} /update " , time () - 24 * 60 * 60 ) ; # 前日の日付で作成 chmod ( " {$datadir} /update " , 0606 ) ; } # 取得済みページの取得 $files = array () ; foreach ( glob ( " {$datadir} /*.dat " ) as $path ) { $name = basename ( $path , ' .dat ' ) ; list ( $date , $id ) = explode ( ' _ ' , $name ) ; $files [ $id ] = $path ; } # 一日に一回更新 $now = explode ( ' . ' , date ( ' Y.m.d ' ) ) ; $limit = date ( ' Ymd ' , mktime ( 0 , 0 , 0 , $now [ 1 ] , $now [ 2 ] - 1 , $now [ 0 ] ) ) ; $update = date ( ' Ymd ' , filemtime ( " {$datadir} /update " ) ) ; if ( $limit >= $update ) { # 全ページのIDを格納する $list = array () ; # 一覧ページの取得 $buff = @ file_get_contents ( " http://tenvi.nexon.co.jp/community/free.asp " ) ; if ( ! $buff ) { print " free.aspページが開けません。詳しくは、TENVIサイトを見てください。 " ; # 記事一覧よりIDの抽出 if ( ! preg_match_all ( ' /<span class="link-ttl"><a href=".*?idx=(\d+).*?">/ ' , $buff , $match ) ) { break ; } foreach ( $match [ 1 ] as $id ) $list [ $id ] = true ; } # 各ページの取得 foreach ( $list as $id => $flag ) { if ( file_exists ( $files [ $id ] ) ) continue ; $buff = @ file_get_contents ( " http://tenvi.nexon.co.jp/community/free_view.asp?bid=tenvifree&idx= {$id} " ) ; if ( ! $buff ) continue ; $data = array ( ' id ' => $id ) ; # タイトルの抽出 if ( ! preg_match ( ' /<td style="word-break:break-all">(.+?)<\/td>/ ' , $buff , $match ) ) continue ; $data [ ' title ' ] = $match [ 1 ] ; # 日付の抽出 if ( ! preg_match ( ' /<div style="padding-right:25px;"><b>.*?</b>(\d+)\D+(\d+)\D+(\d+).*?<\/div>/ ' , $buff , $match ) ) continue ; $data [ ' date ' ] = mktime ( 0 , 0 , 0 , $match [ 2 ] , $match [ 3 ] , $match [ 1 ] ) ; // 抽出文字を切り取る if ( mb_strlen ( $data [ ' title ' ]) > 15 ) { $data [ ' title ' ] = mb_substr ( $data [ ' title ' ] , 0 , 15 ) . ' ... ' ; } # データの保存 $path = sprintf ( ' %s/%s_%s.dat ' , $datadir , date ( ' Ymd ' , $data [ ' date ' ] ) , $data [ ' id ' ] ) ; $fp = @ fopen ( $path , ' w ' ) ; if ( ! $fp ) { print " ディレクトリパーミッションが間違えている可能性があります。書き込みができません。 " ; exit ; } fwrite ( $fp , serialize ( $data ) ) ; fclose ( $fp ) ; chmod ( $path , 0606 ) ; $files [ $data [ ' id ' ]] = $path ; } touch ( " {$datadir} /update " ) ; } # ここからは好きなように $id = isset ( $_GET [ ' id ' ] ) ? intval ( $_GET [ ' id ' ] ) : 0 ; $mode = '' ; if ( $id ) { if ( ! isset ( $files [ $id ] ) ) { print " データ取得に失敗しました。 " ; exit ; } $buff = @ file_get_contents ( $files [ $id ] ) ; if ( ! $buff ) { print " ファイルパーミッションが間違えている可能性があります。オープンできません。 " ; exit ; } $view = unserialize ( $buff ) ; if ( ! $view || ! is_array ( $view ) ) { print " データーの一部が壊れています。表示できません。 " ; unlink ( $files [ $id ] ) ; exit ; } $mode = ' view ' ; } else { rsort ( $files ) ; $files = array_slice ( $files , 0 , 10 ) ; $list = array () ; foreach ( $files as $path ) { $buff = @ file_get_contents ( $path ) ; if ( ! $buff ) continue ; $data = unserialize ( $buff ) ; if ( ! $data || ! is_array ( $data ) ) { unlink ( $files [ $id ] ) ; continue ; } $list [] = $data ; } $mode = ' list ' ; } ?> <?php if ( $mode == ' view ' ) { ?> <?php print date ( ' Y.m.d ' , $view [ ' date ' ] ) ; ?> <?php print $view [ ' title ' ] ; ?> <?php } else { ?> <?php foreach ( $list as $item ) { ?> <div class="bottom-line"> <?php print date ( ' Y.m.d ' , $item [ ' date ' ] ) ; ?> | <a target="_blank" class="Information" href="http://tenvi.nexon.co.jp/community/free_view.asp?bid=tenvifree&idx= <?php print $item [ ' id ' ] ; ?> "> <?php print $item [ ' title ' ] ; ?> </a></div> <?php } ?> <?php } ?>
