Doc.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. if (strpos($this->rule, '$json') !== false) {
  26. $this->type = 'json';
  27. } else {
  28. $this->type = 'dom';
  29. }
  30. }
  31. private function url($url)
  32. {
  33. $this->url = $url;
  34. $value = parse_url($this->url);
  35. $this->host = $value['scheme'] . '://' . $value['host'];
  36. }
  37. public function get()
  38. {
  39. $doc = $this->doc();
  40. if ($this->rule) {
  41. $doc = $this->find($doc, $this->rule);
  42. }
  43. return $doc;
  44. }
  45. public function doc($url = false)
  46. {
  47. $url = $url ? $url : $this->url;
  48. $html = $this->download($url);
  49. return ($this->getClass())::init($html);
  50. }
  51. private function download($url)
  52. {
  53. $this->addLog($url . '下载中...');
  54. $download = new Download($url);
  55. $this->addLog($url . '下载完成');
  56. return $download->get();
  57. }
  58. private function collect($data, $include, $exclude, $filter)
  59. {
  60. if ($include && !preg_match('/' . $include . '/i', $data)) {
  61. return 'error';
  62. }
  63. if ($exclude && preg_match('/' . $exclude . '/i', $data)) {
  64. return 'error';
  65. }
  66. if ($filter) {
  67. $filter = explode("\n", str_replace("\r", '', $filter));
  68. foreach ($filter as $k => $v) {
  69. $data = preg_replace('/' . $v . '/i', '', $data);
  70. }
  71. }
  72. return $data;
  73. }
  74. private function getClass()
  75. {
  76. return 'Spider\\Lib\\Doc\\' . ucfirst($this->type);
  77. }
  78. public function find($doc, $rule)
  79. {
  80. return ($this->getClass())::find($doc, $rule);
  81. }
  82. public function init($data)
  83. {
  84. if (is_string($data) && filter_var($data, FILTER_VALIDATE_URL) !== false) {
  85. $data = $this->doc($data);
  86. } else {
  87. $data = ($this->getClass())::init($data);
  88. }
  89. return $data;
  90. }
  91. public function rule($data, $col, $config)
  92. {
  93. $name = '字段[' . $config['name'] . '('.$config['key'].')]' . '"';
  94. $this->addLog($name . '正在按照规则['.$config['collect_rule'].']进行解析');
  95. $method = 'rule_' . $this->type;
  96. $data = ($this->getClass())::rule($this, $data, $col, $config['collect_rule'], $config['key']);
  97. $this->addLog($name . '解析完成');
  98. return $this->collect($data, $config['collect_include'], $config['collect_exclude'], $config['collect_filter']);
  99. }
  100. public function getUrl($data, $col, $config)
  101. {
  102. $url = $this->rule($data, $col, $config);
  103. if (strpos($url, 'http') === false) {
  104. if ($url[0] == '/') {
  105. $url = $this->host . $url;
  106. } else {
  107. $url = $this->url . $url;
  108. }
  109. }
  110. return $url;
  111. }
  112. public function addLog($string)
  113. {
  114. if ($this->log) {
  115. $this->log->add($string);
  116. }
  117. }
  118. public function saveLog()
  119. {
  120. if ($this->log) {
  121. $this->log->save();
  122. }
  123. }
  124. public function outLog()
  125. {
  126. if ($this->log) {
  127. $this->log->out();
  128. }
  129. }
  130. public function log(Log $log)
  131. {
  132. $this->log = $log;
  133. }
  134. }