Doc.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. public function doc($url = false)
  41. {
  42. $url = $url ? $url : $this->url;
  43. $html = $this->download($url);
  44. if (strpos($this->rule, '$json') !== false) {
  45. $this->type = 'json';
  46. } else {
  47. $this->type = 'dom';
  48. }
  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 rule($data, $col, $config)
  83. {
  84. $name = '字段[' . $config['name'] . '('.$config['key'].')]' . '"';
  85. $this->addLog($name . '正在按照规则['.$config['collect_rule'].']进行解析');
  86. $method = 'rule_' . $this->type;
  87. $data = ($this->getClass())::rule($this, $data, $col, $config['collect_rule'], $config['key']);
  88. $this->addLog($name . '解析完成');
  89. return $this->collect($data, $config['collect_include'], $config['collect_exclude'], $config['collect_filter']);
  90. }
  91. public function getUrl($data, $col, $config)
  92. {
  93. $url = $this->rule($data, $col, $config);
  94. if (strpos($url, 'http') === false) {
  95. if ($url[0] == '/') {
  96. $url = $this->host . $url;
  97. } else {
  98. $url = $this->url . $url;
  99. }
  100. }
  101. return $url;
  102. }
  103. public function addLog($string)
  104. {
  105. if ($this->log) {
  106. $this->log->add($string);
  107. }
  108. }
  109. public function saveLog()
  110. {
  111. if ($this->log) {
  112. $this->log->save();
  113. }
  114. }
  115. public function outLog()
  116. {
  117. if ($this->log) {
  118. $this->log->out();
  119. }
  120. }
  121. public function log(Log $log)
  122. {
  123. $this->log = $log;
  124. }
  125. }