加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

利用PHP命令行模式采集股票趋势信息

发布时间:2020-12-12 21:40:22 所属栏目:PHP教程 来源:网络整理
导读:话不多说,下面直接来看实现代码。 主要函数只有一个类实现(stock.class.php): public function __construct($stockId){ $this - stockId = $stockId; } private function getUrl(){ return "http://stockpage.10jqka.com.cn/" . $this - stockId . "/"; }

话不多说,下面直接来看实现代码。

主要函数只有一个类实现(stock.class.php):

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,$result) ){
return $result[$itemIndex];
}else{
return "-";
}
}

//趋势的规则都一样,合并
private function qushiPattern($name){
return '<div class="txt-aside">' . $name . ':

s<div class="txt-main">([^<])';
}

//支持的标签
private function name($html){
return $this -> match("([^(<]<em>)(",1);<br /> }<br /> private function score($html){<br /> return $this -> match('<span class="analyze-num">(d+(.d+)?)</span>',$html);<br /> }<br /> private function tips($html){<br /> return $this -> match('<span class="analyze-tips">([^<]</em>)</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 /> ?></pre></p> </div> <p><h3>命令提示符中的调用方法如下(stock.php):</h3></p> <div class="jb51code"> <pre class="brush:php;"> <?php <p>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 /> ?></pre></p> </div> <p>直接使用<code> *php.exe stock.php </code>股票代码即可实现调用,每次输入太长的,可以用批处理简化。</p> <p>将下面的代码保存为 stock.cmd。</p> <div class="jb51code"> <pre class="brush:xhtml;"> @XXXphp.exe stock.php %1</pre> </div> <p><h3>运行结果:</h3></p> <p style="text-align: center"><h3><p class="pic_center"><img src="https://files.52php.cn/file_images/article/201608/20168991224293.png?20167991235" /></p></h3></p> <p>这样就完成了单个股票趋势的采集,如果要采集所有的股票信息,可以保存为批处理文件(batch.cmd)</p> <div class="jb51code"> <pre class="brush:xhtml;"> @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...</pre> </div> <p>双击打开即可显示,如果想保存到文件,可以执行 <code>batch.cmd > log.txt</code>,然后将结果复制到 Execl(或ET)即可进行更负责的分析。</p> <p style="text-align: center"><p class="pic_center"><img src="https://files.52php.cn/file_images/article/201608/20168991337862.png?20167991347" /></p></p> <p>以上就是利用PHP命令行模式采集股票趋势信息的全部内容,这个功能很方便实用,感兴趣的朋友们快快实践起来吧。</p> <p style="text-align:right;">(编辑:李大同)</p> <p style="text-align:right;">【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!</p> </td> </tr> </table> </div> <div class="dede_pagess"><ul class="pagelist"></ul></div> <div class="ad-690"><script src='https://ess.0577qiche.com/d/js/acmsd/ad76.js' language='javascript'></script></div> <div class="g-box10"> <div class="t-2">相关内容</div> <ul class="b-box12"></ul> <ul class="b-box13"><li><a href="https://www.lidatong.com.cn/html/jc/php/2020/1213/89670.html" target="_blank">php标签云的实现代码</a></li><li><a href="https://www.lidatong.com.cn/html/jc/php/2020/1213/116332.html" target="_blank">PHP操作MySQL常见错误信息及分析</a></li><li><a href="https://www.lidatong.com.cn/html/jc/php/2020/1213/96413.html" target="_blank">php – 一个WordPress安装(多站点)或多个安装?</a></li><li><a href="https://www.lidatong.com.cn/html/jc/php/2020/1212/79558.html" target="_blank">php正确输出json数据的实例讲解</a></li><li><a href="https://www.lidatong.com.cn/html/jc/php/2020/1213/142511.html" target="_blank">php – 从数组创建字符串?</a></li><li><a href="https://www.lidatong.com.cn/html/jc/php/2020/1213/106554.html" target="_blank">php – 使用Theme My Login WordPress插件过滤’retrieve_p</a></li><li><a href="https://www.lidatong.com.cn/html/jc/php/2020/1212/77612.html" target="_blank">PHP使用strrev翻转中文乱码问题的解决方法</a></li><li><a href="https://www.lidatong.com.cn/html/jc/php/2020/1213/140263.html" target="_blank">php – 如何获取距离矩阵,计算在我的服务器上脱机</a></li><li><a href="https://www.lidatong.com.cn/html/jc/php/2020/1213/133595.html" target="_blank">php数组操作函数(合并,拆分,追加,查找,删除等)</a></li><li><a href="https://www.lidatong.com.cn/html/jc/php/2020/1213/128058.html" target="_blank">树 二叉树 多叉树</a></li><div class="cl"></div></ul> </div> </div> </div> <div class="right-1 fr"> <div class="g-box8"> <div class="t-2">推荐文章</div> <ul></ul> </div> <div class="g-box11"> <div class="t-2">站长推荐</div> <ul class="b-box7"><li><a href="https://www.lidatong.com.cn/html/jc/php/2020/1212/78986.html" target="_blank">PHP 的Opcache加速的使用方法</a></li><li><a href="https://www.lidatong.com.cn/html/jc/php/2020/1212/73700.html" target="_blank">php实现指定字符串中查找子字符串的方法</a></li><li><a href="https://www.lidatong.com.cn/html/jc/php/2020/1212/78857.html" target="_blank">PHP mysql_result()函数使用方法</a></li><li><a href="https://www.lidatong.com.cn/html/jc/php/2020/1213/134922.html" target="_blank">php字符串函数操作实例</a></li><li><a href="https://www.lidatong.com.cn/html/jc/php/2020/1213/120861.html" target="_blank">PHP – 将多维数组转换为字符串?</a></li><li><a href="https://www.lidatong.com.cn/html/jc/php/2020/1212/75731.html" target="_blank">php图片添加文字水印实现代码</a></li><li><a href="https://www.lidatong.com.cn/html/jc/php/2020/1212/68841.html" target="_blank">php安装xdebug/php安装pear/phpunit详解步骤(图)</a></li><li><a href="https://www.lidatong.com.cn/html/jc/php/2020/1212/71690.html" target="_blank">C/S和B/S两种架构区别与优缺点分析</a></li><li><a href="https://www.lidatong.com.cn/html/jc/php/2020/1213/142207.html" target="_blank">如何检查非关联数组在PHP中是否唯一?</a></li><li><a href="https://www.lidatong.com.cn/html/jc/php/2020/1213/85935.html" target="_blank">PHP实现文件上传和多文件上传</a></li></ul> </div> <script type="text/javascript">jQuery(".g-box8").slide({ titCell:"li",triggerTime:0 }); </script> <div class="ad-250"><script src='https://ess.0577qiche.com/d/js/acmsd/ad103.js' language='javascript'></script></div> <div class="g-box3 u-3"> <div class="t-1">热点阅读</div> <ul class="b-box2"></ul> </div> <div class="ad-250"><script src='https://ess.0577qiche.com/d/js/acmsd/ad77.js' language='javascript'></script></div> </div> <div class="cl"></div> </div> <div class="ad-960"><script src='https://ess.0577qiche.com/d/js/acmsd/ad78.js' language='javascript'></script></div> <div class="footer"> <p>【免责声明】本站内容转载自互联网,其发布内容言论不代表本站观点,如果其链接、内容的侵犯您的权益,烦请提交相关链接至邮箱bqsm@foxmail.com我们将及时予以处理。</p> <p>建议您使用1920×1080分辨率、谷歌浏览器Google Chrome、Microsoft Edge以获得本站的最佳浏览效果</p> <p>Copygight © 2008-2022 https://www.lidatong.com.cn/ All Rights Reserved. 李大同</p> <p><script type="text/javascript" src="//js.users.51.la/21280179.js"></script></p> </div> </body> </html>