PHP command line to collect all stock trend information programs

Source: Internet
Author: User

Recently helped a friend write a simple stock collection tool, collected with the flowers shun stock trend information, using PHP command line mode to write. Only the collection part function is completed, the output data is copied to execl (or ET) analysis, which is more convenient than any backstage.

The primary function has only one class implementation (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 ());
}

Core, the label name is matched by a regular match, and the result of the corresponding method is replaced by the label placeholder
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"); The command prompt encoding for Windows is GBK
}

Private Function Match ($pattern, $html, $itemIndex = 1) {
$pattern = '/'. Str_replace ('/', ' \/', $pattern). '/';
if (Preg_match ($pattern, $html, $result)) {
return $result [$itemIndex];
}else{
Return "-";
}
}

The rules of the trend are the same, merging
Private Function Qushipattern ($name) {
Return ' <div class= ' txt-aside ' > '. $name. ': </div>\s*<div class= "Txt-main" > ([^<]*) </div> ';
}

Supported labels
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 ("Short term trend"), $html);
}
Private Function Qushimiddle ($html) {
Return $this-> match ($this-> qushipattern ("medium-term trend"), $html);
}
Private Function Qushilong ($html) {
Return $this-> match ($this-> Qushipattern ("long-term trend"), $html);
}
}
?>

The calling method in the command prompt is as follows (stock.php):


<?php

if (count ($ARGV) >= 2) {
Require ("stock.class.php");
$stockId = $argv [1];
$stock = new Stockclass ($stockId);
$temp = $stockId;
$temp. = "{name}"; Name
$temp. = "{score}"; Score
$temp. = "{tips}"; Describe
$temp. = "{Qushishort}"; Short-term trends
$temp. = "{Qushimiddle}"; Medium-term trends
$temp. = "{Qushilong}"; Long-term trends
$temp. = "{Zidingyi}"; Custom, add Zidingyi method directly in Stockclass
$temp. = "\ n";
echo $stock-> getInfo ($temp);
}
?>

Direct use of *\php.exe stock.php stock code to implement the call, each input too long, can be simplified by batch processing. Save the following code as Stock.cmd.


@XXX \php.exe stock.php%1
Run Result:

This completes the collection of individual stock trends, and can be saved as a batch file if all stock information is to be collected (Batch.cmd)

1

@echo off
Call the stock 000001
Call the stock 000002
Call the stock 000003
Call the stock 000004
Call the stock 000005
Call the stock 000006
Call the stock 000007
Call the stock ticker code n ...
Double-click to open to display, if you want to save to a file, you can perform Batch.cmd > log.txt, and then copy the results to EXECL (or ET) for more responsible analysis.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.