Dom.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace Spider\Lib\Doc;
  3. include(DEVER_APP_PATH . 'third/phpQuery.php');
  4. use Dever;
  5. use phpQuery;
  6. class Dom
  7. {
  8. public static function init($html)
  9. {
  10. if (strstr($html, 'gb2312')) {
  11. $html = str_replace('gb2312', 'utf-8', $html);
  12. }
  13. return phpQuery::newDocumentHTML($html);
  14. }
  15. public static function find($dom, $rule)
  16. {
  17. list($rule, $attr) = self::each($rule);
  18. $rule = str_replace(array('$', ').', '$dom->find.'), array('$dom->find', ')->', '$dom->'), $rule);
  19. $cmd = '$dom = ' . $rule . ';';
  20. eval($cmd);
  21. return self::findAttr($dom, $attr);
  22. }
  23. public static function each($rule)
  24. {
  25. $attr = '';
  26. if (strpos($rule, '.each().') !== false) {
  27. $temp = explode('.each()', $rule);
  28. $rule = $temp[0];
  29. $attr = '$' . $temp[1];
  30. }
  31. return array($rule, $attr);
  32. }
  33. public static function findAttr($dom, $attr)
  34. {
  35. if (!$attr) {
  36. return $dom;
  37. }
  38. $data = array();
  39. foreach ($dom as $k => $v) {
  40. $data[] = self::find(pq($v), $attr);
  41. }
  42. return json_encode($data, JSON_UNESCAPED_UNICODE);
  43. }
  44. public static function get($data)
  45. {
  46. return pq($data);
  47. }
  48. public static function rule($doc, $dom, $col, $rule, $key)
  49. {
  50. $result = Core::rule($doc, $dom, $col, $rule, $key);
  51. return $result;
  52. }
  53. }