抓取豆瓣電影TOP250的PHP代碼
for ($start = 0; $start < 250; $start += 25) { $url = "http://movie.douban.com/top250?start=$start&filter=&type="; $titles = parsePage($url); if ($titles === false) { echo $url, "\n"; } else { array_walk($titles, 'printTitle'); } }function parsePage($url) { $html = file_get_contents($url); if ($html === false) { return false; }
if (preg_match_all('/<a.+?<span class="title">([^<]+)/s', $html, $matches) === false) { return false; } $titles = array(); foreach($matches[1] as $item) { $titles[] = iconv('utf-8', 'gbk', $item); } return $titles;
}
$count = 0; function printTitle($title) { global $count; ++$count; printf("%3d %s\n", $count, $title); }</pre>
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!