使用php进行批量抓取QQ空间相册链接

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

使用php进行批量抓取QQ空间相册链接插图

手动验证了一下相册数与抓到的链接数,一个都不差,/宾果

源码

<?php
/**
 * QzoneImgDown
 * QQ空间相册图片地址批量获取
 * @author Youngxj
 * @DateTime 2019-12-01
 * @Blog youngxj
 */
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   Youngxj
     * @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   Youngxj
     * @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   Youngxj
     * @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不方便下载)

转自Young小杰

①本站资源来源网络或者用户投稿,切勿私自传播于网络,否则将追究法律责任。且仅供学习交流之用,如有侵权请联系站长删除。
②本站所有资源均来自网络转载,版权归原作者和公司所有,如果有侵犯到您的权益,请第一时间联系邮箱:821425543@qq.com 我们将配合处理!
③转载本站资源,请注明出处古韵资源网
④如果资源失效或下载链接错误请联系站长QQ821425543。
⑤如果分享的账号无法使用了请站长QQ821425543进行更换。
古韵资源网 » 使用php进行批量抓取QQ空间相册链接

发表评论