Import requests
Import Pymongo
Import requests
Import OS
Class Qqgroup:
#三个接口url get QQ group number get each group member information get QQ Avatar
Group_url = "Https://qun.qq.com/cgi-bin/qun_mgr/get_group_list"
Member_url = "Https://qun.qq.com/cgi-bin/qun_mgr/search_group_members"
Pic_url = "http://q4.qlogo.cn/g?b=qq&nk={%s}&s=140"
#一般将cookie与链接数据库代码写在构造函数
def __init__ (Self,cookie):
Self.cookie = Cookie
Self.client = Pymongo. Mongoclient (host= ' 118.24.3.40 ', port=27017)
self.db = self.client[' Qq_group '] #数据库名称
self.table = self.db[' Qq_group '] #表名称
#获取 QQ Group number
def get_all_group (self):
data = {' BKN ': 2039193070}
res = requests.post (self.group_url,data,headers={' cookie ': Self.cookie}). JSON ()
#新方法 1, for multiple list merges for the return value, if return is empty, increase the default value to null 2, and the Get method for dictionary values
all_groups = Res.get (' Join ', []) + res.get (' Create ', []) + res.get (' Manage ', [])
All_gc=[i.get (' GC ') for I in All_groups]
Return ALL_GC
#获取每组成员信息
def get_group_info (Self,num):
data = {"GC": num, "St": 0, "End": $, "sort": 0, "BKN": 2039193070}
res = Requests.post (Url=self.member_url, Data=data, headers={' Cookie ': Self.cookie})
MEMS = Res.json (). Get ("MEMS")
For I in MEMS:
MEM = {
"QQ": I.get ("UIn"),
"Gender": I.get ("G"),
"Nick": I.get ("Nick"),
"Card": I.get ("card"),
"Qage": I.get ("Qage")
}
If Mem.get ("gender") = = 0:
mem[' gender ' = "male"
Elif Mem.get ("gender") = = 1:
mem[' gender ' = "female"
Else
mem[' gender ' = "Unknown"
If Mem.get ("card") = = "":
mem[' card ' = "no Group card"
Self.save_mongo (MEM)
Self.down_pic (I.get ("UIn"))
#存入mogodb数据库
def Save_mongo (Self,data):
Self.table.insert (data)
#下载qq头像
def down_pic (Self,qq_num):
Real_url=self.pic_url%qq_num
Res=requests.get (real_url). Content
Pathnew = Os.path.dirname (Os.path.abspath (__file__))
Pathnew = Os.path.join (pathnew, ' IMGs ')
If not Os.path.isdir (' IMGs '):
Os.makedirs (' IMGs ')
With open (Os.path.join (pathnew, str (qq_num) + '. jpg '), ' WB ') as FW:
Fw.write (RES)
Print ('%s avatar download complete '% qq_num)
#main函数用于将各过程统一到一个函数, easy to instantiate and invoke
def mian (self):
All_group=self.get_all_group ()
For I in All_group:
Self.get_group_info (i)
#实例化类 parameter passed as a cookie
Q=qqgroup (' pgv_pvid=1745803612; pgv_pvi=9485686784; Rk=vrzxxdy1y7; _qpsvr_localtk=0.8876927078641847; pgv_si=s3971407872; PTISP=CNC; PTCZ=E42F943C39B23E3D7A94D9DEAC0DE69388506BBB9B09000F197B8681624F70AC; uin=o0106148088; [Email protected]; pt2gguin=o0106148088; p_uin=o0106148088; Pt4_token=vrwh5lwxxuae0oaji5dr-xet8f13t5oheqfwvi3cjjq_; P_skey=spanebpdwjmfagplmvt-msrmpteypnpykjrjo80kxqy_ ')
Q.mian ()
python-crawler: Take QQ number in the group member information into the database, and the QQ avatar download saved to the folder, the image named QQ number (example 3)