Python2.7获取QQ照片墙

通过wireshark抓电脑版QQ的http数据包得到的结果

接口调用:

http://taotao.qq.com/cgi-bin/photo_wall_cgi_list?uin=10001&g_tk=112308395&hostUin=qqnumber&pos=0&num=400&lastfilekey=&lasttime=0&photo_source=1&requester=1&format=json

现在照片墙还没有上限,你可以将num=400改得尽量大,接口中的uin表示你自己的QQ,g_tk需要计算得出,实际上这两个参数服务器不会校验,可以任意取,也不需要带cookies

如果没有照片返回结果是这样的

{"code":0,"data":{"next_offset":-1,"total":0,"uin":123456789,"uptime":1397711082},"message":"","subcode":0}
如果有照片,返回结果是这样的

{"code":0,"data":{"next_offset":-1,"photo_list":[{"burl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/44c23f8ad0f5d5e0c15fcf39d14267e081cd4e2091\/640","ctime":1446960924,"filekey":"44c23f8ad0f5d5e0c15fcf39d14267e081cd4e2091","murl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/44c23f8ad0f5d5e0c15fcf39d14267e081cd4e2091\/160","ourl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/44c23f8ad0f5d5e0c15fcf39d14267e081cd4e2091\/0","surl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/44c23f8ad0f5d5e0c15fcf39d14267e081cd4e2091\/100"},{"burl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/e1a706fd3d78430d5c0e3d46ae76770581cd4e2091\/640","ctime":1446960912,"filekey":"e1a706fd3d78430d5c0e3d46ae76770581cd4e2091","murl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/e1a706fd3d78430d5c0e3d46ae76770581cd4e2091\/160","ourl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/e1a706fd3d78430d5c0e3d46ae76770581cd4e2091\/0","surl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/e1a706fd3d78430d5c0e3d46ae76770581cd4e2091\/100"},{"burl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/1b3740ab132fec2567d281a6ae4ce65b81cd4e2091\/640","ctime":1446960903,"filekey":"1b3740ab132fec2567d281a6ae4ce65b81cd4e2091","murl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/1b3740ab132fec2567d281a6ae4ce65b81cd4e2091\/160","ourl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/1b3740ab132fec2567d281a6ae4ce65b81cd4e2091\/0","surl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/1b3740ab132fec2567d281a6ae4ce65b81cd4e2091\/100"},{"burl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/bf318cb2191b3a6300acf8cde68f21c681cd4e2091\/640","ctime":1412657386,"filekey":"bf318cb2191b3a6300acf8cde68f21c681cd4e2091","murl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/bf318cb2191b3a6300acf8cde68f21c681cd4e2091\/160","ourl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/bf318cb2191b3a6300acf8cde68f21c681cd4e2091\/0","surl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/bf318cb2191b3a6300acf8cde68f21c681cd4e2091\/100"},{"burl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/f3659f9cecef0cf36feea13022f4786881cd4e2091\/640","ctime":1411656582,"filekey":"f3659f9cecef0cf36feea13022f4786881cd4e2091","murl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/f3659f9cecef0cf36feea13022f4786881cd4e2091\/160","ourl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/f3659f9cecef0cf36feea13022f4786881cd4e2091\/0","surl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/f3659f9cecef0cf36feea13022f4786881cd4e2091\/100"},{"burl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/c0d3c192c33c54e978f01ec49690265481cd4e2091\/640","ctime":1411656581,"filekey":"c0d3c192c33c54e978f01ec49690265481cd4e2091","murl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/c0d3c192c33c54e978f01ec49690265481cd4e2091\/160","ourl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/c0d3c192c33c54e978f01ec49690265481cd4e2091\/0","surl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/c0d3c192c33c54e978f01ec49690265481cd4e2091\/100"},{"burl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/288d4e40e8635b229c36c99c9869c50381cd4e2091\/640","ctime":1411656541,"filekey":"288d4e40e8635b229c36c99c9869c50381cd4e2091","murl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/288d4e40e8635b229c36c99c9869c50381cd4e2091\/160","ourl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/288d4e40e8635b229c36c99c9869c50381cd4e2091\/0","surl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/288d4e40e8635b229c36c99c9869c50381cd4e2091\/100"},{"burl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/115617c351047bef607a86f68373ee3581cd4e2091\/640","ctime":1411656539,"filekey":"115617c351047bef607a86f68373ee3581cd4e2091","murl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/115617c351047bef607a86f68373ee3581cd4e2091\/160","ourl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/115617c351047bef607a86f68373ee3581cd4e2091\/0","surl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/115617c351047bef607a86f68373ee3581cd4e2091\/100"},{"burl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/b6e7e988888ace6b4d12d7ffc32f2ca781cd4e2091\/640","ctime":1397190206,"filekey":"b6e7e988888ace6b4d12d7ffc32f2ca781cd4e2091","murl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/b6e7e988888ace6b4d12d7ffc32f2ca781cd4e2091\/160","ourl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/b6e7e988888ace6b4d12d7ffc32f2ca781cd4e2091\/0","surl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/b6e7e988888ace6b4d12d7ffc32f2ca781cd4e2091\/100"},{"burl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/56bf5ea1f5096671cf365d56ab63ade781cd4e2091\/640","ctime":1397190195,"filekey":"56bf5ea1f5096671cf365d56ab63ade781cd4e2091","murl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/56bf5ea1f5096671cf365d56ab63ade781cd4e2091\/160","ourl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/56bf5ea1f5096671cf365d56ab63ade781cd4e2091\/0","surl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/56bf5ea1f5096671cf365d56ab63ade781cd4e2091\/100"},{"burl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/7acfe7c6c2e48b5b22de8e32086726d881cd4e2091\/640","ctime":1397190180,"filekey":"7acfe7c6c2e48b5b22de8e32086726d881cd4e2091","murl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/7acfe7c6c2e48b5b22de8e32086726d881cd4e2091\/160","ourl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/7acfe7c6c2e48b5b22de8e32086726d881cd4e2091\/0","surl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/7acfe7c6c2e48b5b22de8e32086726d881cd4e2091\/100"},{"burl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/82f7911a6b9a458e6ced939223964b4581cd4e2091\/640","ctime":1390318130,"filekey":"82f7911a6b9a458e6ced939223964b4581cd4e2091","murl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/82f7911a6b9a458e6ced939223964b4581cd4e2091\/160","ourl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/82f7911a6b9a458e6ced939223964b4581cd4e2091\/0","surl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/82f7911a6b9a458e6ced939223964b4581cd4e2091\/100"},{"burl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/4a2e9886148db7689dcf88a9d128f72681cd4e2091\/640","ctime":1375022218,"filekey":"4a2e9886148db7689dcf88a9d128f72681cd4e2091","murl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/4a2e9886148db7689dcf88a9d128f72681cd4e2091\/160","ourl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/4a2e9886148db7689dcf88a9d128f72681cd4e2091\/0","surl":"http:\/\/ugc.qpic.cn\/mqq_photo\/0\/4a2e9886148db7689dcf88a9d128f72681cd4e2091\/100"}],"total":13,"uin":123456789,"uptime":1446960924},"message":"","subcode":0}
我们关注total和ourl字段就可以了,total表示照片总数,ourl表示原图,其它的url是缩放过的图

老规矩,你要查的qq号做个无BOM的utf-8编码的txt放同目录下,每个QQ号占一行,最后一个QQ号需要再加一个或多个空行,文件格式根据文件头智能识别

代码如下

# -*- coding: UTF-8 -*-
import httplib2
import json
import os
import shutil
import time
import binascii

dir='QQWallPhotos/'  
try:  
    os.makedirs(dir)  # 建立相应的文件夹  
except:  
    shutil.rmtree(dir)  # 无论文件夹是否为空都移除该文件夹  
    os.makedirs(dir)  
  
fp = open('qqlist.txt', 'r')  
qqlist = fp.readlines() 

for i in range(len(qqlist)):  
    qqlist[i] = qqlist[i][:-1]  
fp.close()  

h = httplib2.Http(timeout=6) 
for target in qqlist:
    
    url = 'http://taotao.qq.com/cgi-bin/photo_wall_cgi_list?uin=10001&g_tk=112308395&hostUin=qqnumber&pos=0&num=400&lastfilekey=&lasttime=0&photo_source=1&requester=1&format=json' 
    if len(target) == 0 :  # 防止因为出现空行删除所有照片  
        continue
    try:  
        os.makedirs(dir + target)  # 建立相应的文件夹  
    except :  
        shutil.rmtree(dir + target)  # 无论文件夹是否为空都移除该文件夹  
        os.makedirs(dir + target)    
    print(u'当前QQ:' + target)  
    url=url.replace('qqnumber',target) 
    resp = None
    content = None
    for t1 in (1,6):
        try:
            resp, content = h.request(url)
            break
        except:
            print("-----socket timout:",url)
            continue
    print content
    try:
        output = json.loads(content)  # json字符串转字典
    except:
        break # json字符串转字典出现异常,退出循环
    print output 
    if output['code'] != 0 :  # 返回结果异常,退出本次循环 
        continue   
    total = output['data']['total'] # 照片墙总数,如果为0表示没有照片
    print total
    if total != 0 :
        for i in range(0, len(output['data']['photo_list'])): 
            ctime = output['data']['photo_list'][i]['ctime']
            filename =  time.strftime("%Y%m%d%H%M%S",time.localtime(ctime))
            url =  output['data']['photo_list'][i]['ourl']
            print url
            resp = None
            content = None            
            for t2 in (1,6):
                try:
                    resp, content = h.request(url)
                    break
                except:
                    print("-----socket timout:",url)
                    continue                
            fmt = binascii.b2a_hex(content[0:4]) # 读取前4字节转化为16进制字符串
            print fmt  
            phototype = {'47494638': '.gif', 'ffd8ffe0': '.jpg', 'ffd8ffe1': '.jpg', 'ffd8ffdb': '.jpg', '89504e47': '.png'}  # 智能识别文件格式             
            qualified_file_name = dir + target + '/' + filename + phototype[fmt]  
            open(qualified_file_name, 'wb').write(content)
    if total == 0 :
        shutil.rmtree(dir + target)
print(u'下载完成') 



更新1:增加ffd8ffdb的jpg格式

更新2:修复连接超时导致下载中断的异常

更新3:加入超时重发3次请求的机制,根据笔者监测到的情况,超时设为6秒时,超时一般出现在获取json的时候,笔者300好友数最多重发两次解决问题,总重发次数在10次以内

更新4:修复变量名命名不当以及变量未清空导致获取到上一次结果的问题

你可能感兴趣的:(python,qq,照片墙)