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 명령 행 모드 를 이용 하여 주식 트 렌 드 정 보 를 수집 하 는 모든 내용 입 니 다.이 기능 은 매우 편리 하고 실 용적 입 니 다.관심 있 는 친구 들 은 빨리 실천 하 세 요.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Laravel - 변환된 유효성 검사 규칙으로 API 요청 제공동적 콘텐츠를 위해 API를 통해 Laravel CMS에 연결하는 모바일 앱(또는 웹사이트) 구축을 고려하십시오. 이제 앱은 CMS에서 번역된 콘텐츠를 받을 것으로 예상되는 다국어 앱이 될 수 있습니다. 일반적으로 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.