欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

利用PHP命令行模式采集股票趨勢(shì)信息

 更新時(shí)間:2016年08月09日 09:13:41   投稿:daisy  
本文介紹的是一個(gè)簡(jiǎn)單的股票采集工具,采集的同花順股票趨勢(shì)信息,使用php命令行模式編寫(xiě)。只完成了采集部分功能,將輸出的數(shù)據(jù)復(fù)制到execl(或ET)分析,比任何后臺(tái)都方便。下面一起來(lái)學(xué)習(xí)學(xué)習(xí)。

話不多說(shuō),下面直接來(lái)看實(shí)現(xiàn)代碼。

主要函數(shù)只有一個(gè)類實(shí)現(xiàn)(stock.class.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());
 }
 
 //核心,通過(guò)正則匹配出 標(biāo)簽名,并將對(duì)應(yīng)的方法的結(jié)果替換掉標(biāo)簽占位符
 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 "-";
  }
 }
 
 //趨勢(shì)的規(guī)則都一樣,合并
 private function qushiPattern($name){
  return '<div class="txt-aside">' . $name . ':</div>\s*<div class="txt-main">([^<]*)</div>';
 }
 
 //支持的標(biāo)簽
 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("短期趨勢(shì)"), $html);
 }
 private function qushimiddle($html){
  return $this -> match($this -> qushiPattern("中期趨勢(shì)"), $html);
 }
 private function qushilong($html){
  return $this -> match($this -> qushiPattern("長(zhǎng)期趨勢(shì)"), $html);
 }
 }
?>

命令提示符中的調(diào)用方法如下(stock.php):

<?php
 
 if(count($argv) >= 2){
 require("stock.class.php");
 $stockId = $argv[1];
 $stock = new StockClass($stockId);
 $temp = $stockId;
 $temp .= " {name}"; //名稱
 $temp .= " {score}"; //評(píng)分
 $temp .= " {tips}"; //描述
 $temp .= " {qushishort}"; //短期趨勢(shì)
 $temp .= " {qushimiddle}"; //中期趨勢(shì)
 $temp .= " {qushilong}"; //長(zhǎng)期趨勢(shì)
 //$temp .= " {zidingyi}"; //自定義,直接在StockClass增加zidingyi方法即可
 $temp .= "\n";
 echo $stock -> getInfo($temp);
 }
?>

直接使用 *\php.exe stock.php 股票代碼即可實(shí)現(xiàn)調(diào)用,每次輸入太長(zhǎng)的,可以用批處理簡(jiǎn)化。

將下面的代碼保存為 stock.cmd。

@XXX\php.exe stock.php %1

運(yùn)行結(jié)果:

這樣就完成了單個(gè)股票趨勢(shì)的采集,如果要采集所有的股票信息,可以保存為批處理文件(batch.cmd)

@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...

雙擊打開(kāi)即可顯示,如果想保存到文件,可以執(zhí)行 batch.cmd > log.txt,然后將結(jié)果復(fù)制到 Execl(或ET)即可進(jìn)行更負(fù)責(zé)的分析。

以上就是利用PHP命令行模式采集股票趨勢(shì)信息的全部?jī)?nèi)容,這個(gè)功能很方便實(shí)用,感興趣的朋友們快快實(shí)踐起來(lái)吧。

相關(guān)文章

最新評(píng)論