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

2025-05-29 0 101

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

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62
<?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):

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18
<?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 .= "\\n";

echo $stock -> getinfo($temp);

}

?>

直接使用 *\\php.exe stock.php 股票代码即可实现调用,每次输入太长的,可以用批处理简化。

将下面的代码保存为 stock.cmd。

?

1
@xxx\\php.exe stock.php %1

运行结果:

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

这样就完成了单个股票趋势的采集,如果要采集所有的股票信息,可以保存为批处理文件(batch.cmd)

?

1

2

3

4

5

6

7

8

9
@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命令行模式采集股票趋势信息

以上就是利用php命令行模式采集股票趋势信息的全部内容,这个功能很方便实用,感兴趣的朋友们快快实践起来吧。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 利用PHP命令行模式采集股票趋势信息 https://www.kuaiidc.com/96802.html

相关文章

发表评论
暂无评论