{
    分享网正式开通,我们为大家提供免费资源,欢迎大家踊跃投稿!

使用PHP批量抓取QQ空间相册链接

使用PHP批量抓取QQ空间相册链接

前言:

使用PHP批量抓取QQ空间相册链接,如果对你有帮助就看看吧。

正文:

在网上无意看到的文章《为什么QQ空间与我们渐行渐远?》里面就提到为了保留回忆嘛人们都会会抽空备份QQ空间的照片,但是在网上找了很久也没有找到一个有效的工具,所以在这里给大家分享一下通过PHP来实现的方法,也希望能帮到有同样诉求的朋友。

Ps:采用QQ空间api,有效时间不确定,如果发现api生效,请自己抓取api。

PS:手动验证了一下相册数与抓到的链接数,一个都不差。

实例代码:

<?php
/**
 * QzoneImgDown
 * QQ空间相册图片地址批量获取
 * @DateTime 2019-12-01
  */
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;

    }

    /**
     * 获取图片列表
     * @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);
        }
    }

    /**
     * 计算翻页
     * @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');
        }

    }

    /**
     * 获取相册列表
     * @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   Youngxj
     * @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);
    }
}

Ps:如何获取cookie及uin就不再赘述了 该类库仅用于获取自己QQ空间照片链接,至于QQ好友相册没有适配 获取的下载链接请使用下载工具进行下载(php不方便下载)


资源均来自第三方,谨慎下载,前往第三方网站下载 解压密码:www.xkwo.com 解压密码:www.xkwo.com 解压密码:www.xkwo.com 解压密码:www.xkwo.com 解压密码:www.xkwo.com 解压密码:www.xkwo.com 解压密码:www.xkwo.com 解压密码:www.xkwo.com


米微资源分享网 , 版权所有丨本站资源仅限于学习研究,严禁从事商业或者非法活动!丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:使用PHP批量抓取QQ空间相册链接
喜欢 ()分享 (0)