欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

抓取豆瓣电影TOP250的PHP代码

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

PHP