Use the PHP Command Line Mode to collect stock trend information, and the php Command Line

Source: Internet
Author: User

Use the PHP Command Line Mode to collect stock trend information, and the php Command Line

Let's just talk about the implementation code.

The main 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, use regular expressions to match the tag name, replace the result of the corresponding method with 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 Windows Command Prompt code is GBK} private function match ($ pattern, $ html, $ itemIndex = 1) {$ pattern = '/'. str_replace ('/', '\/', $ pattern ). '/'; if (preg_match ($ pattern, $ html, $ result) {ret Urn $ result [$ itemIndex];} else {return "-" ;}}// all the rules of the trend are the same, and the private function qushiPattern ($ name) is merged) {return '<div class = "txt-aside"> '. $ name. ': </div> \ s * <div class = "txt-main"> ([^ <] *) </div> ';} // The supported tag 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-> qushiPatt Ern ("long-term trends"), $ html) ;}}?>

The call 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}"; // description $ temp. = "{qushishort}"; // short-term trend $ temp. = "{qushimiddle}"; // medium trend $ temp. = "{qushilong}"; // long-term trend // $ temp. = "{zidingyi}"; // custom, add the zidingyi method directly in StockClass to $ temp. = "\ n"; echo $ stock-> getInfo ($ Temp) ;}?>

Direct use *\php.exe stock.php The stock code can be called. Each input is too long and can be simplified using batch processing.

Save the following code as stock. cmd.

@XXX\php.exe stock.php %1

Running result:

This completes the collection of individual stock trends. If you want to collect all stock information, you can save it as a batch processing file (batch. cmd)

@ Echo offcall stock 000001 call stock 000002 call stock 000003 call stock 000004 call stock 000005 call stock 000006 call stock 000007 call stock Code n...

Double-click it to display it. To save it to a file, runbatch.cmd > log.txtAnd then copy the results to Execl (or ET) for more responsible analysis.

The above is the use of PHP Command Line Mode to collect all the content of the stock trend information, this function is very convenient and practical, interested friends to practice it quickly.

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.