Doc.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. namespace Spider\Lib;
  3. use Dever;
  4. class Doc
  5. {
  6. private $url;
  7. private $host;
  8. private $path;
  9. private $rule;
  10. public $type = 'dom';
  11. private $log = false;
  12. private $cur = '';
  13. static protected $instance;
  14. static public function getInstance($url, $rule = '')
  15. {
  16. $key = $url . md5($rule);
  17. if (empty(self::$instance[$key])) {
  18. # 此处开协程:Dever::coroutine(self::$instance[$key]);
  19. self::$instance[$key] = new self($url, $rule);
  20. }
  21. return self::$instance[$key];
  22. }
  23. public function __construct($url, $rule = '')
  24. {
  25. $this->url($url);
  26. $this->rule = $rule;
  27. if (strpos($this->rule, '$json') !== false) {
  28. $this->type = 'json';
  29. } else {
  30. $this->type = 'dom';
  31. }
  32. }
  33. private function url($url = false)
  34. {
  35. if (!$url) {
  36. return;
  37. }
  38. $this->url = $url;
  39. $value = parse_url($this->url);
  40. $this->path = $this->host = $value['scheme'] . '://' . $value['host'];
  41. if (isset($value['path']) && $value['path']) {
  42. $temp = explode('/', $value['path']);
  43. unset($temp[count($temp)-1]);
  44. $this->path .= implode('/', $temp);
  45. }
  46. $this->path .= '/';
  47. }
  48. public function get($param = array())
  49. {
  50. $doc = $this->doc(false, $param);
  51. if (!$this->cur) {
  52. $this->cur = $doc;
  53. }
  54. if ($this->rule) {
  55. $doc = $this->find($doc, $this->rule);
  56. }
  57. return $doc;
  58. }
  59. public function doc($url = false, $param = array())
  60. {
  61. $this->url($url);
  62. $html = $this->download($this->url, $param);
  63. return ($this->getClass())::init($html);
  64. }
  65. public function getCur()
  66. {
  67. return $this->cur;
  68. }
  69. private function download($url, $header = '', $param = '')
  70. {
  71. $this->addLog($url . '下载中...');
  72. $download = new Download($url, $header, $param);
  73. $this->addLog($url . '下载完成');
  74. return $download->get($this->type);
  75. }
  76. private function collect($data, $include, $exclude, $filter, $attr)
  77. {
  78. if ($include) {
  79. $include = Dever::split($include);
  80. foreach ($include as $k => $v) {
  81. $state = preg_match('/' . $v . '/i', $data);
  82. if ($state) {
  83. break;
  84. }
  85. }
  86. if (!$state) {
  87. return 'error';
  88. }
  89. }
  90. if ($exclude) {
  91. $exclude = Dever::split($exclude);
  92. foreach ($exclude as $k => $v) {
  93. $state = preg_match('/' . $v . '/i', $data);
  94. if (!$state) {
  95. return 'error';
  96. }
  97. }
  98. }
  99. if ($filter) {
  100. $filter = Dever::split($filter);
  101. foreach ($filter as $k => $v) {
  102. $s = '';
  103. if (strstr($v, '=>')) {
  104. $temp = explode('=>', $v);
  105. $v = $temp[0];
  106. $s = $temp[1];
  107. }
  108. $data = preg_replace('/' . $v . '/i', $s, $data);
  109. }
  110. }
  111. if ($attr) {
  112. $attr = Dever::split($attr);
  113. foreach ($attr as $k => $v) {
  114. $s = '';
  115. $v = $v . '="(.*?)"';
  116. $data = preg_replace('/' . $v . '/i', $s, $data);
  117. }
  118. }
  119. return $data;
  120. }
  121. private function getClass()
  122. {
  123. return 'Spider\\Lib\\Doc\\' . ucfirst($this->type);
  124. }
  125. public function find($doc, $rule)
  126. {
  127. return ($this->getClass())::find($doc, $rule);
  128. }
  129. public function init($data)
  130. {
  131. if (is_string($data) && filter_var($data, FILTER_VALIDATE_URL) !== false) {
  132. $data = $this->doc($data);
  133. } else {
  134. $data = ($this->getClass())::init($data);
  135. }
  136. return $data;
  137. }
  138. public function rule($data, $col, $config)
  139. {
  140. $name = '字段[' . $config['name'] . '('.$config['key'].')]' . '"';
  141. $this->addLog($name . '正在按照规则['.$config['collect_rule'].']进行解析');
  142. $method = 'rule_' . $this->type;
  143. $result = $this->getRule($data, $col, $config['collect_rule'], $config['key']);
  144. if (isset($config['collect_url']) && $config['collect_url']) {
  145. $collect_url = Dever::split($config['collect_url']);
  146. if (!isset($collect_url[1])) {
  147. $collect_url[1] = '';
  148. }
  149. $temp = array();
  150. $temp[] = $result;
  151. $this->getNext($temp, $data, $col, $collect_url[0], $collect_url[1], $config['collect_rule'], $config['key']);
  152. $result = implode(',', $temp);
  153. }
  154. $this->addLog($name . '解析完成');
  155. return $this->collect($result, $config['collect_include'], $config['collect_exclude'], $config['collect_filter'], $config['collect_attr']);
  156. }
  157. public function getNext(&$result, $data, $col, $collect_url, $collect_include, $collect_rule, $key)
  158. {
  159. $url = $this->getUrl($data, $col, $collect_url, $key);
  160. if ($url) {
  161. if ($collect_include && !strstr($url, $collect_include)) {
  162. return;
  163. }
  164. $data = $this->init($url);
  165. if ($data) {
  166. $temp = $this->getRule($data, $col, $collect_rule, $key);
  167. if ($temp) {
  168. $result[] = $temp;
  169. $this->getNext($result, $data, $col, $collect_url, $collect_include, $collect_rule, $key);
  170. }
  171. }
  172. }
  173. }
  174. public function getUrl($data, $col, $collect_rule, $key)
  175. {
  176. $url = $this->getRule($data, $col, $collect_rule, $key);
  177. if (!$url) {
  178. return '';
  179. }
  180. if (strpos($url, 'http') === false) {
  181. if ($url[0] == '/') {
  182. $url = $this->host . $url;
  183. } elseif (strstr($url, '.')) {
  184. $url = $this->path . $url;
  185. } else {
  186. $url = $this->url . $url;
  187. }
  188. }
  189. return $url;
  190. }
  191. public function getRule($data, $col, $collect_rule, $key)
  192. {
  193. return ($this->getClass())::rule($this, $data, $col, $collect_rule, $key);
  194. }
  195. public function addLog($string)
  196. {
  197. if ($this->log) {
  198. $this->log->add($string);
  199. }
  200. }
  201. public function saveLog()
  202. {
  203. if ($this->log) {
  204. $this->log->save();
  205. }
  206. }
  207. public function outLog()
  208. {
  209. if ($this->log) {
  210. $this->log->out();
  211. }
  212. }
  213. public function log(Log $log)
  214. {
  215. $this->log = $log;
  216. }
  217. }