萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> 用php抓取百度貼吧指定相冊圖片

用php抓取百度貼吧指定相冊圖片

 <?php

@set_time_limit(10);
//貼吧名稱
$tbname = "mugen";
//相冊ID
$tid = "2124904411";
//圖冊頁url模版
$galleryurltpl = "http://tieba.baidu.com/photo/bw/picture/guide?kw=%s&tid=%s";
//本地的目錄
$savepath = "R:/images/";
//帖子子文件夾
$filedir = $savepath.$tid;
//圖片文件
$filenametpl = $filedir."/%s.jpg";
//圖冊頁url
$galleryurl = sprintf($galleryurltpl, $tbname, $tid);

//返回的json數據
$retjson = file_get_contents($galleryurl);
$retarray = json_decode($retjson, true);
//圖片列表
$piclist = $retarray['data']['pic_list'];

//目錄是否存在
if(!is_dir($filedir))
mkdir($filedir);
foreach($piclist as $pic){
$pic_id = $pic['img']['original']['id'];
$url = "http://imgsrc.baidu.com/forum/pic/item/".$pic_id.".jpg";
$filename = sprintf($filenametpl, $pic_id);
//下載圖片
$imagebin = file_get_contents($url);
//保存圖片
file_put_contents($filename,$imagebin);
}
?>

copyright © 萬盛學電腦網 all rights reserved