Doc.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace Spider\Lib;
  3. use Dever;
  4. class Doc
  5. {
  6. private $url;
  7. private $host;
  8. private $rule;
  9. public $type = 'dom';
  10. private $log = false;
  11. static protected $instance;
  12. static public function getInstance($url, $rule = '')
  13. {
  14. $key = $url . md5($rule);
  15. if (empty(self::$instance[$key])) {
  16. # 此处开协程:Dever::coroutine(self::$instance[$key]);
  17. self::$instance[$key] = new self($url, $rule);
  18. }
  19. return self::$instance[$key];
  20. }
  21. public function __construct($url, $rule = '')
  22. {
  23. $this->url($url);
  24. $this->rule = $rule;
  25. }
  26. private function url($url)
  27. {
  28. $this->url = $url;
  29. $value = parse_url($this->url);
  30. $this->host = $value['scheme'] . '://' . $value['host'];
  31. }
  32. public function get()
  33. {
  34. $doc = $this->doc();
  35. if ($this->rule) {
  36. $doc = $this->find($doc, $this->rule);
  37. }
  38. return $doc;
  39. }
  40. private function doc()
  41. {
  42. $html = $this->download($this->url);
  43. if (strpos($this->rule, '$json') !== false) {
  44. $this->type = 'json';
  45. } else {
  46. $this->type = 'dom';
  47. }
  48. return ($this->getClass())::init($html);
  49. }
  50. private function download($url)
  51. {
  52. $this->addLog($this->url . '下载中...');
  53. $download = new Download($url);
  54. $this->addLog($this->url . '下载完成');
  55. return $download->get();
  56. }
  57. private function collect($data, $include, $exclude, $filter)
  58. {
  59. if ($include && !preg_match('/' . $include . '/i', $data)) {
  60. return 'error';
  61. }
  62. if ($exclude && preg_match('/' . $exclude . '/i', $data)) {
  63. return 'error';
  64. }
  65. if ($filter) {
  66. $data = preg_replace('/' . $filter . '/i', '', $data);
  67. }
  68. return $data;
  69. }
  70. private function getClass()
  71. {
  72. return 'Spider\\Lib\\Doc\\' . ucfirst($this->type);
  73. }
  74. public function find($doc, $rule)
  75. {
  76. return ($this->getClass())::find($doc, $rule);
  77. }
  78. public function rule($data, $col, $config)
  79. {
  80. $name = '字段[' . $config['name'] . '('.$config['key'].')]' . '"';
  81. $this->addLog($name . '正在按照规则['.$config['collect_rule'].']进行解析');
  82. $method = 'rule_' . $this->type;
  83. $data = ($this->getClass())::rule($this, $data, $col, $config['collect_rule'], $config['key']);
  84. $this->addLog($name . '解析完成');
  85. return $this->collect($data, $config['collect_include'], $config['collect_exclude'], $config['collect_filter']);
  86. }
  87. public function getUrl($data, $col, $config)
  88. {
  89. $url = $this->rule($data, $col, $config);
  90. if (strpos($url, 'http') === false) {
  91. if ($url[0] == '/') {
  92. $url = $this->host . $url;
  93. } else {
  94. $url = $this->url . $url;
  95. }
  96. }
  97. return $url;
  98. }
  99. public function addLog($string)
  100. {
  101. if ($this->log) {
  102. $this->log->add($string);
  103. }
  104. }
  105. public function saveLog()
  106. {
  107. if ($this->log) {
  108. $this->log->save();
  109. }
  110. }
  111. public function outLog()
  112. {
  113. if ($this->log) {
  114. $this->log->out();
  115. }
  116. }
  117. public function log(Log $log)
  118. {
  119. $this->log = $log;
  120. }
  121. }