请选择 进入手机版 | 继续访问电脑版

★我要吧★

 找回密码
 注册[Register]
搜索
qq空间相册密码查看为什么登陆后需要激活无法注册?

[分享] 使用php批量抓取QQ空间相册链接教程

[复制链接]
发表于 2020-8-10 14:33:23 | 显示全部楼层 |阅读模式
搬运别人的,有需要可以看看。。。看着有点麻烦
1 u3 t' i' j0 T$ a9 f想备份QQ空间的照片,但是在网上找了很久也没有找到一个有效的工具。0 z+ s* j: L5 r7 Q% j8 g
作为一个Phper,盘就完事了,所以顶着寒冷,搞了三个小时终于把QQ空间所有照片备份了。3 v% V  h% M5 Y. ~, m# n/ ]
希望能帮到有同样诉求的朋友,所以公开代码。6 j, q7 L& Z. d. ~" g
提示:采用QQ空间api,有效时间不确定,如果发现api失效,请自己抓取api。
: e2 i* u2 K, S" o5 O, |手动验证了一下相册数与抓到的链接数,一个都不差。
2 h! ?" C! E8 M! d7 x* n* {源码代码如下:( f; k" F9 E$ m; J) E8 I3 v
<?php/** * QzoneImgDown * QQ空间相册图片地址批量获取 * @author  * @DateTime 2019-12-01 * @Blog */class QzoneImgDown{    // 调用方法    // 请登录https://h5.qzone.qq.com/获取你的cookie以及g_tk和uin    // $g_tk    = '';    // $res_uin = ''; // 该uin为你的QQ号(常规情况下)    // $cookie  = '';    // $qzone   = new QzoneImgDown($g_tk, $uin, $cookie);    // echo $qzone->getList();    // Ps:该类库仅用于获取自己QQ空间照片链接,至于QQ好友相册没有适配    // 获取的下载链接请使用下载工具进行下载(php不方便下载)    public function __construct($g_tk, $res_uin, $cookie)    {        $this->g_tk    = $g_tk;        $this->res_uin = $res_uin;        // 相册列表接口        $this->listApi = 'https://mobile.qzone.qq.com/list?g_tk=' . $this->g_tk . '&format=json&list_type=album&action=0&res_uin=' . $this->res_uin . '&count=99';        // 相册图片列表接口        $this->imgApi = 'https://h5.qzone.qq.com/webapp/json/mqzone_photo/getPhotoList2?g_tk=' . $this->g_tk . '&uin=' . $this->res_uin . '&albumid=xxxxxxxxxxxxx&ps=0&pn=20&password=&password_cleartext=0&swidth=1080&sheight=1920';        $this->cookie = $cookie;    }    /**     * 获取图片列表     * @Author       * @DateTime 2019-12-01     * @param    [type]     $url 相册ajax数据     * @return   [type]          [description]     */    private function getImg($url)    {        // echo $url;exit;        $json = $this->curl_request($url, '', '', $this->cookie);        $arr  = json_decode($json, 1);        if (isset($arr['data']['album']['name'])) {            $fileName = $arr['data']['album']['name'];            $myfile   = @fopen($fileName . '.txt', 'a+');            if(!$myfile){                exit('文件写入失败,请检查目录读写权限是否正常');            }            $photos = array_values($arr['data']['photos']);            // echo json_encode($arr['data']['photos']);exit;            foreach ($photos as $key => $value) {                foreach ($photos[$key] as $keys => $values) {                    // var_dump($key);var_dump($keys);                    // var_dump($photos[$key][$keys]['1']['url']);                    echo $text = $photos[$key][$keys]['1']['url'] . "\n";                    fwrite($myfile, $text);                }            }            fclose($myfile);        }    }    /**     * 计算翻页     * @Author       * @DateTime 2019-12-01     * @param    [type]     $url [description]     * @return   [type]          [description]     */    private function ret($url)    {        $json = $this->curl_request($url, '', '', $this->cookie);        $arr  = json_decode($json, 1);        // echo json_encode($arr);exit;        if (isset($arr['data']['album']['name'])) {            $total_count = $arr['data']['total_count'];            $page_count  = ceil($total_count / 20);            // var_dump($total_count);exit;            for ($i = 0; $i <= $page_count; $i++) {                // var_dump($i);                $url = $this->url_set_value($url, 'ps', $i * 20);                // var_dump($url);exit;                $this->getImg($url);            }            // var_dump($page_count);exit;            // exit;        } else {            exit('error');        }    }    /**     * 获取相册列表     * @Author   我要吧www.abc369.net     * @DateTime 2019-12-01     * @return   [type]     [description]     */    public function getList()    {        $json = $this->curl_request($this->listApi, '', '', $this->cookie);        $arr  = json_decode($json, 1);        if (isset($arr['data']['vFeeds'])) {            $newArr = $arr['data']['vFeeds'];            foreach ($newArr as $key => $value) {                $albumid = $newArr[$key]['pic']['albumid'];                $url     = $this->url_set_value($this->imgApi, 'albumid', $albumid);                // echo $url."\n";                echo $this->ret($url);            }        } else {            exit('error');        }    }    /**     * curl模拟提交     * @param  [type]  $url          访问的URL     * @param  string  $post         post数据(不填则为GET)     * @param  string  $referer      自定义来路     * @param  string  $cookie       提交的$cookies     * @param  integer $returnCookie 是否返回$cookies     * @param  string  $ua           自定义UA     * @return [type]                [description]     */    private function curl_request($url, $post = '', $referer = '', $cookie = '', $returnCookie = 0, $ua = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0')    {        $curl = curl_init();        curl_setopt($curl, CURLOPT_URL, $url);        curl_setopt($curl, CURLOPT_USERAGENT, $ua);        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);        curl_setopt($curl, CURLOPT_AUTOREFERER, 1);        curl_setopt($curl, CURLOPT_TIMEOUT, 60);        curl_setopt($curl, CURLOPT_REFERER, $referer);        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);        $httpheader[] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";        $httpheader[] = "Accept-Encoding:gzip, deflate";        $httpheader[] = "Accept-Language:zh-CN,zh;q=0.9";        $httpheader[] = "Connection:close";        curl_setopt($curl, CURLOPT_HTTPHEADER, $httpheader);        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);        if ($post) {            curl_setopt($curl, CURLOPT_POST, 1);            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));        }        if ($cookie) {            curl_setopt($curl, CURLOPT_COOKIE, $cookie);        }        curl_setopt($curl, CURLOPT_HEADER, $returnCookie);        curl_setopt($curl, CURLOPT_TIMEOUT, 10);        curl_setopt($curl, CURLOPT_ENCODING, "gzip");        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);        $data = curl_exec($curl);        if (curl_errno($curl)) {            return curl_error($curl);        }        curl_close($curl);        if ($returnCookie) {            list($header, $body) = explode("\r\n\r\n", $data, 2);            preg_match_all("/Set\-Cookie:([^;]*);/", $header, $matches);            $info['cookie']  = substr($matches[1][1], 1);            $info['content'] = $body;            return $info;        } else {            return $data;        }    }    /**     * 替换get参数     * @Author       * @DateTime 2019-12-01     * @param    [type]     $url   地址     * @param    [type]     $key   key     * @param    [type]     $value val     * @return   [type]            [description]     */    private function url_set_value($url, $key, $value)    {        $a     = explode('?', $url);        $url_f = $a[0];        $query = $a[1];        parse_str($query, $arr);        $arr[$key] = $value;        return $url_f . '?' . http_build_query($arr);    }}
+ o) A' f& [- T; ?' p& pPs:如何获取cookie及uin就不再赘述了,该类库仅用于获取自己QQ空间照片链接,至于QQ好友相册没有适配,获取的下载链接请使用下载工具进行下载。(php不方便下载)
. ^7 Q' Z4 c0 k7 D- k
& H6 d: p+ I$ E8 z2 A) U% f: Z5 E; k2 j
发表于 2020-9-4 00:59:45 | 显示全部楼层
哇,技术贴必须赞一下
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

QQ|手机版|小黑屋|☆我要吧☆ ( 豫ICP备13016831号-1 )

GMT+8, 2024-3-28 17:40 , Processed in 0.104971 second(s), 18 queries .

Powered by abc369 X3.4

© 2001-2023 abc369.

快速回复 返回顶部 返回列表