使用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小杰

免责声明                      

本站提供的资源转载自国内外各大媒体和网络,不提供任何游戏破解资源;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。我们非常重视版权问题,如有侵权请邮件与我们联系处理。敬请谅解!E-mail:oohu1234@qq.com

经验教程

nginx简易防CC攻击策略规则

2019-12-4 17:31:19

经验教程

PCQQv9.1.8防撤回勋章墙破解补丁下载,本人亲测有效

2019-12-10 14:34:20

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧