Home > Backend Development > PHP Tutorial > Capture the PHP code of the top 250 Douban movies

Capture the PHP code of the top 250 Douban movies

WBOY
Release: 2016-07-23 08:54:50
Original
1952 people have browsed it
  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 %sn", $count, $title);
  29. }
复制代码

PHP


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template