Python2.7获取QQ空间部分好友

来源:互联网 发布:jquery ui.min.js 1.8 编辑:程序博客网 时间:2024/05/17 14:25

首先需要安装curl工具,然后复制curl

参考http://blog.csdn.net/gsls200808/article/details/46933307

浏览器打开http://user.qzone.qq.com/[QQ号码]/myhome/friends,复制friend_ship_manager.cgi为curl

获取的是我在意谁和谁在意我的json列表,QQ空间对我在意谁的好友数目限制是200,谁在意我的好友数目限制是200,代码把两个列表全部获取了,但是通过这个方法有个缺陷,当好友数大于200时,无法获取完整的好友列表

代码如下

# -*- coding: UTF-8 -*-import osimport shleximport subprocessimport json# 添加curl的环境变量os.putenv('PATH', 'D:\\curl-7.33.0-win64-nossl\\;' + os.getenv('PATH'))# 浏览器打开http://user.qzone.qq.com/[QQ号码]/myhome/friends,复制friend_ship_manager.cgi为curl# 我在意谁do=1,谁在意我do=2origin_friend = 'friend_ship_manager.cgi'origin_friend = origin_friend.replace('--compressed ', '')curl = origin_friendargs = shlex.split(curl)result = subprocess.check_output(args).decode('utf-8')jsonstr = result[result.find('(') + 1: result.find(')', -1) - 1]  # json字符串,去除不标准的json数据output = json.loads(jsonstr)  # 最终json数据items_list=output['data']['items_list']qqlist = set()for item in items_list:    qqlist.add(item['uin'])# 获取谁在意我origin_friend_whocare = origin_friendcurl = origin_friend_whocare.replace('do=1', 'do=2')args = shlex.split(curl)result = subprocess.check_output(args).decode('utf-8')jsonstr = result[result.find('(') + 1: result.find(')', -1) - 1]  # json字符串,去除不标准的json数据output = json.loads(jsonstr)  # 最终json数据items_list=output['data']['items_list']for item in items_list:    qqlist.add(item['uin'])# 打印信息并输出到文件print '获取QQ号个数'+str(len(qqlist))filename='qqlist.txt'fileobj = open(filename, 'w')result = [str(qq) + '\n' for qq in sorted(qqlist)]fileobj.writelines(result)fileobj.flush()fileobj.close()print '写入完成'


0 0