当前位置:Gxlcms > PHP教程 > 抓取豆瓣电影TOP250的PHP代码

抓取豆瓣电影TOP250的PHP代码

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

  1. for ($start = 0; $start < 250; $start += 25) {
  2. $url = "http://movie.douban.com/top250?start=$start&filter=&type=";
  3. $titles = parsePage($url);
  4. if ($titles === false) {
  5. echo $url, "\n";
  6. } else {
  7. array_walk($titles, 'printTitle');
  8. }
  9. }
  10. function parsePage($url) {
  11. $html = file_get_contents($url);
  12. if ($html === false) {
  13. return false;
  14. }
  15. if (preg_match_all('/([^<]+)/s', $html, $matches) === false) {
  16. return false;
  17. }
  18. $titles = array();
  19. foreach($matches[1] as $item) {
  20. $titles[] = iconv('utf-8', 'gbk', $item);
  21. }
  22. return $titles;
  23. }
  24. $count = 0;
  25. function printTitle($title) {
  26. global $count;
  27. ++$count;
  28. printf("%3d %s\n", $count, $title);
  29. }

PHP

人气教程排行