PHP命令行パターンを利用して株の動向情報を収集する
多くの話をしないで、直接コードを実現します。
主な関数は一つのカテゴリーのみです。
下のコードをstock.cmdとして保存します。
これで単一株式のトレンドの採集が完了しました。すべての株情報を収集するなら、バッチファイルとして保存できます。
以上はPHP命令行パターンを利用して株のトレンド情報を収集したものです。この機能はとても便利で実用的です。興味のある方は早速実践してみてください。
主な関数は一つのカテゴリーのみです。
<?php
class StockClass{
public $stockId;
public function __construct($stockId){
$this -> stockId = $stockId;
}
private function getUrl(){
return "http://stockpage.10jqka.com.cn/" . $this -> stockId . "/";
}
private function getPage(){
return file_get_contents($this -> getUrl());
}
// , ,
public function getInfo($template){
$html = $this -> getPage();
if( preg_match_all("/\{([^\}]*)\}/", $template, $result) ){
foreach($result[1] as $index => $fun){
$template = str_replace($result[0][$index], $this -> $fun($html), $template);
}
}
return mb_convert_encoding($template, "GBK", "UTF-8"); //Windows GBK
}
private function match($pattern, $html, $itemIndex = 1){
$pattern = '/' . str_replace('/', '\/', $pattern) . '/';
if( preg_match($pattern, $html, $result) ){
return $result[$itemIndex];
}else{
return "-";
}
}
// ,
private function qushiPattern($name){
return '<div class="txt-aside">' . $name . ':</div>\s*<div class="txt-main">([^<]*)</div>';
}
//
private function name($html){
return $this -> match("<title>([^\(<]*)\(", $html, 1);
}
private function score($html){
return $this -> match('<span class="analyze-num">(\d+(\.\d+)?)</span>', $html);
}
private function tips($html){
return $this -> match('<span class="analyze-tips">([^<]*)</span>', $html);
}
private function qushishort($html){
return $this -> match($this -> qushiPattern(" "), $html);
}
private function qushimiddle($html){
return $this -> match($this -> qushiPattern(" "), $html);
}
private function qushilong($html){
return $this -> match($this -> qushiPattern(" "), $html);
}
}
?>
コマンドプロンプトの呼び出し方法は以下の通りです。
<?php
if(count($argv) >= 2){
require("stock.class.php");
$stockId = $argv[1];
$stock = new StockClass($stockId);
$temp = $stockId;
$temp .= " {name}"; //
$temp .= " {score}"; //
$temp .= " {tips}"; //
$temp .= " {qushishort}"; //
$temp .= " {qushimiddle}"; //
$temp .= " {qushilong}"; //
//$temp .= " {zidingyi}"; // , StockClass zidingyi
$temp .= "
";
echo $stock -> getInfo($temp);
}
?>
*\php.exe stock.php
株コードを直接使用すれば、呼び出しが可能です。入力が長すぎると、バッチ処理で簡単にできます。下のコードをstock.cmdとして保存します。
@XXX\php.exe stock.php %1
実行結果:これで単一株式のトレンドの採集が完了しました。すべての株情報を収集するなら、バッチファイルとして保存できます。
@echo off
call stock 000001
call stock 000002
call stock 000003
call stock 000004
call stock 000005
call stock 000006
call stock 000007
call stock n...
ダブルクリックして開くと表示されます。ファイルに保存したいなら、batch.cmd > log.txt
を実行して、結果をExecl(またはET)にコピーすれば、より責任ある分析が可能です。以上はPHP命令行パターンを利用して株のトレンド情報を収集したものです。この機能はとても便利で実用的です。興味のある方は早速実践してみてください。