当前位置:Gxlcms > PHP教程 > php基于Snoopy解析网页html的方法_PHP

php基于Snoopy解析网页html的方法_PHP

时间:2021-07-01 10:21:17 帮助过:32人阅读

本文实例讲述了php基于Snoopy解析网页html的方法。分享给大家供大家参考。具体实现方法如下:

  1. set_time_limit(0);
  2. $user = array('20517', '20518');
  3. header("content-Type: text/html; charset=utf-8");
  4. require_once './Snoopy.php';
  5. $snoopy = new Snoopy();
  6. //$uri = 'http://www.juzimi.com/meitumeiju';
  7. $uri = 'http://www.juzimi.com/meitumeiju?page=1';
  8. $snoopy->fetch($uri);
  9. $con = $snoopy->getResults();
  10. $doc = new DOMDocument();
  11. $doc->loadHTML($con);
  12. /* make a result array ... */
  13. $result = [];
  14. /* go through all nodes which have class="baby" ... */
  15. $i = 0;
  16. foreach( ( new DOMXPath( $doc ) )->query( '//*[@id="bdshare"]' ) as $element ) {
  17. $row = $doc->saveHTML($element);
  18. $start = stripos($row, '{');
  19. $end = stripos($row, '}');
  20. $len = $end - $start + 1;
  21. $json = substr($row, $start, $len);
  22. $json = str_replace("'", '"', $json);
  23. $obj = json_decode($json);
  24. $pic = $obj->pic;
  25. $desc = $obj->desc;
  26. $filename = '/Uploads/meiju/card' . date('ymdhis') . $i . '.png';
  27. $file = '/alidata/www/test' . $filename;
  28. $image = file_get_contents($pic);
  29. file_put_contents($file, $image);
  30. }

希望本文所述对大家的php程序设计有所帮助。

人气教程排行