一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

PHP命令行采集所有股票趋势信息程序

时间:2022-06-24 17:46:02 编辑:袖梨 来源:一聚教程网

最近帮朋友写了一个简单的股票采集工具,采集的同花顺股票趋势信息,使用php命令行模式编写。只完成了采集部分功能,将输出的数据复制到execl(或ET)分析,比任何后台都方便。

主要函数只有一个类实现(stock.class.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 '

' . $name . ':
s*
([^<]*)
';
        }
        
        //支持的标签
        private function name($html){
            return $this -> match("([^(<]*)(", $html, 1);<br />         }<br />         private function score($html){<br />             return $this -> match('<span class="analyze-num">(d+(.d+)?)</span>', $html);<br />         }<br />         private function ti<a href="/fw/photo.html" target="_blank">ps</a>($html){<br />             return $this -> match('<span class="analyze-tips">([^<]*)</span>', $html);<br />         }<br />         private function qushishort($html){<br />             return $this -> match($this -> qushiPattern("短期趋势"), $html);<br />         }<br />         private function qushimiddle($html){<br />             return $this -> match($this -> qushiPattern("中期趋势"), $html);<br />         }<br />         private function qushilong($html){<br />             return $this -> match($this -> qushiPattern("长期趋势"), $html);<br />         }<br />     }<br /> ?></p> <p>命令提示符中的调用方法如下(stock.php):</p> <p><br /> <?php<br />      <br />     if(count($argv) >= 2){<br />         require("stock.class.php");<br />         $stockId = $argv[1];<br />         $stock = new StockClass($stockId);<br />         $temp = $stockId;<br />         $temp .= "  {name}";    //名称<br />         $temp .= "  {score}";   //评分<br />         $temp .= "  {tips}";    //描述<br />         $temp .= "  {qushishort}";  //短期趋势<br />         $temp .= "  {qushimiddle}"; //中期趋势<br />         $temp .= "  {qushilong}";   //长期趋势<br />         //$temp .= "    {zidingyi}";    //自定义,直接在StockClass增加zidingyi方法即可<br />         $temp .= "n";<br />         echo $stock -> getInfo($temp);<br />     }<br /> ?></p> <p>直接使用 *php.exe stock.php 股票代码即可实现调用,每次输入太长的,可以用批处理简化。将下面的代码保存为 stock.cmd。</p> <p><br /> @XXXphp.exe stock.php %1<br /> 运行结果:</p> <p> </p> <center><a href="https://img.111cn.net/uploads/20220923/img_632dad7b74ff130.jpg" class="js-smartPhoto-pc" target="_blank"><img alt="" src="https://img.111cn.net/uploads/20220923/img_632dad7b74ff130.jpg" /></a></center> <p>这样就完成了单个股票趋势的采集,如果要采集所有的股票信息,可以保存为批处理文件(batch.cmd)</p> <p>1</p> <p>@echo off<br /> call stock 000001<br /> call stock 000002<br /> call stock 000003<br /> call stock 000004<br /> call stock 000005<br /> call stock 000006<br /> call stock 000007<br /> call stock 股票代码n...<br /> 双击打开即可显示,如果想保存到文件,可以执行 batch.cmd > log.txt,然后将结果复制到 Execl(或ET)即可进行更负责的分析。</p> <center><a href="https://img.111cn.net/uploads/20220923/img_632dad7b9fc1831.jpg" class="js-smartPhoto-pc" target="_blank"><img alt="" src="https://img.111cn.net/uploads/20220923/img_632dad7b9fc1831.jpg" /></a></center> <div class="articles"> <div class="tit02"> <h4>相关文章</h4> </div> <ul> <li> <a target="_blank" href="/new/422339.htm">人们熟悉的寄居蟹属于以下哪种分类 神奇海洋11月21日答案</a> <span>11-21</span> </li> <li> <a target="_blank" href="/new/422338.htm">第五人格11.22共研服有什么更新 11月22日共研服更新内容介绍</a> <span>11-21</span> </li> <li> <a target="_blank" href="/new/422337.htm">原神恰斯卡怎么培养</a> <span>11-21</span> </li> <li> <a target="_blank" href="/new/422336.htm">无期迷途四星装束是谁</a> <span>11-21</span> </li> <li> <a target="_blank" href="/new/422335.htm">王者荣耀帝丹高中校服怎么获得</a> <span>11-21</span> </li> <li> <a target="_blank" href="/new/422334.htm">光遇姆明季后续版本怎么玩</a> <span>11-21</span> </li> </ul> </div> </div> <div class="pages art-detail"> </div> </div> </div> </div> </div> <div class="hot-column"> <div class="cont"> <div class="tit"> <h4>热门栏目</h4> </div> <ul class="clearfix"> <li> <h6><a href="/list-1/" target="_blank">php教程</a></h6> <a href="/list-45/" target="_blank">php入门</a> <a href="/list-46/" target="_blank">php安全</a> <a href="/list-47/" target="_blank">php安装</a> <a href="/list-48/" target="_blank">php常用代码</a> <a href="/list-49/" target="_blank">php高级应用</a> </li> <li> <h6><a href="/list-2/" target="_blank">asp.net教程</a></h6> <a href="/list-78/" target="_blank">基础入门</a> <a href="/list-79/" target="_blank">.Net开发</a> <a href="/list-80/" target="_blank">C语言</a> <a href="/list-81/" target="_blank">VB.Net语言</a> <a href="/list-82/" target="_blank">WebService</a> </li> <li> <h6><a href="/list-6/" target="_blank">手机开发</a></h6> <a href="/list-208/" target="_blank">安卓教程</a> <a href="/list-209/" target="_blank">ios7教程</a> <a href="/list-210/" target="_blank">Windows Phone</a> <a href="/list-211/" target="_blank">Windows Mobile</a> <a href="/list-212/" target="_blank">手机常见问题</a> </li> <li> <h6><a href="/list-3/" target="_blank">css教程</a></h6> <a href="/list-99/" target="_blank">CSS入门</a> <a href="/list-100/" target="_blank">常用代码</a> <a href="/list-101/" target="_blank">经典案例</a> <a href="/list-102/" target="_blank">样式布局</a> <a href="/list-103/" target="_blank">高级应用</a> </li> <li> <h6><a href="/list-4/" target="_blank">网页制作</a></h6> <a href="/list-136/" target="_blank">设计基础</a> <a href="/list-137/" target="_blank">Dreamweaver</a> <a href="/list-138/" target="_blank">Frontpage</a> <a href="/list-139/" target="_blank">js教程</a> <a href="/list-140/" target="_blank">XNL/XSLT</a> </li> <li> <h6><a href="/list-7/" target="_blank">办公数码</a></h6> <a href="/list-236/" target="_blank">word</a> <a href="/list-237/" target="_blank">excel</a> <a href="/list-238/" target="_blank">powerpoint</a> <a href="/list-239/" target="_blank">金山WPS</a> <a href="/list-240/" target="_blank">电脑新手</a> </li> <li> <h6><a href="/list-11/" target="_blank">jsp教程</a></h6> <a href="/list-68/" target="_blank">Application与Applet</a> <a href="/list-69/" target="_blank">J2EE/EJB/服务器</a> <a href="/list-70/" target="_blank">J2ME开发</a> <a href="/list-71/" target="_blank">Java基础</a> <a href="/list-72/" target="_blank">Java技巧及代码</a> </li> </ul> </div> </div> <div class="footer"> <div class="cont"> <p> <a href="/" target="_self">一聚教程网</a>| <a href="javascript:;" class="about" target="_self">关于我们</a>| <a href="javascript:;" class="contact" target="_self">联系我们</a>| <a href="javascript:;" class="gg_contact" target="_self">广告合作</a>| <a href="javascript:;" class="friend_link" target="_self">友情链接</a>| <a href="javascript:;" class="copyright_notice" target="_self">版权声明</a> </p> <p> <span>copyRight@2007-2022 www.111CN.NET AII Right Reserved <a href="https://beian.miit.gov.cn/" target="_blank" class="beian"></a></span> </p> <p> <span> 网站内容来自网络整理或网友投稿如有侵权行为请邮件:111cn.com@163.com 我们24小时内处理 </span> </p> </div> </div> <script> var advData = {"img_fixed_pc_adv":"https:\/\/img.111cn.net\/uploads\/20240509\/663c2e9729f58.jpg","img_fixed_mob_adv":"https:\/\/img.111cn.net\/uploads\/20240509\/663c2e8793225.jpg","url_adv":"http:\/\/shop.hushen.cn\/shop\/c\/baojianpin.html","str_adv":"\u864e\u795e\u5546\u57ce\uff1a\u5173\u7231\u7537\u6027\uff0c\u66f4\u61c2\u7537\u4eba\u3002\u89e3\u51b3\u5927\u4f17\u7684\u7537\u8a00\u4e4b\u9690","img_popup_adv":"https:\/\/img.111cn.net\/uploads\/20240509\/663c2e748238d.png","pc_show_img":"2","pc_show_popup":"2","pc_show_video":"2","mob_show_img":"2","mob_show_popup":"2","mob_show_video":"2","close_adv":"https:\/\/img.111cn.net\/uploads\/20240508\/663b20650801e.png","video_adv":"\/pc\/images\/pc-adv.mp4"}; </script> <script src="/jspc/func.js" type="text/javascript"></script> <!-- Google tag (gtag.js) --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-DSRRGRV1TL"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-DSRRGRV1TL'); </script> <script src="/js/stat.js"></script> </body> </html>