PHP 명령 행 모드 로 주식 트 렌 드 정보 수집

4033 단어 php채집 하 다.
말 이 많 지 않 으 니 실현 코드 를 직접 보 겠 습 니 다.
주요 함 수 는 하나의 클래스 만 실 현 됩 니 다(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());
 }
 
 //  ,           ,                  
 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);
 }
 }
?>
명령 프롬프트 의 호출 방법 은 다음 과 같 습 니 다(stock.php).

<?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
실행 결과:

이렇게 하면 단일 주식 트 렌 드 의 수집 을 완 료 했 습 니 다.모든 주식 정 보 를 수집 하려 면 일괄 처리 파일(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...
더 블 클릭 으로 열 면 파일 에 저장 하려 면 실행batch.cmd > log.txt한 다음 결 과 를 Execl(또는 ET)로 복사 하면 더 책임감 있 는 분석 이 가능 하 다 는 것 을 보 여 준다.

이상 은 PHP 명령 행 모드 를 이용 하여 주식 트 렌 드 정 보 를 수집 하 는 모든 내용 입 니 다.이 기능 은 매우 편리 하고 실 용적 입 니 다.관심 있 는 친구 들 은 빨리 실천 하 세 요.

좋은 웹페이지 즐겨찾기