Python-获取QQ群成员信息并存入excel及redis并发送邮件

Posted huoxn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python-获取QQ群成员信息并存入excel及redis并发送邮件相关的知识,希望对你有一定的参考价值。

‘‘‘
需求:
  1、请求qq群的接口,实现传入一个群号
  2、把群里每个人的 昵称、群备注、入群时间、qq号,性别,存到redis里面,用hash类型
    {"qq_num":XXX,"nick":"XXX","card":"XXX","gender":"男","入群时间":"2017-01-03"}
  3、把每个人的头像下载下来保存到本地,XXX.jpg
  4、把昵称、群备注、入群时间、qq号,性别这些信息写到excel里面
  5、把excel当做附件发到邮箱里面。
‘‘‘

import json
import os
import requests,redis,xlwt,yagmail,time

def getData():  #获取群接口的信息
    url = XXXXXXXX # 获取头像的url
    data = {
        gc: 921144696,
        st: 0,
        end: 20,
        sort: 0,
        bkn: 341160307
    }
    headers = {cookie:RK=JQZpwBp1by; ptcz=6c30e26a9ed6be93d3de9e4c4aca3e55650cf99fcffa64729bd1d58a5fb209d9; pgv_pvi=779236352; pgv_pvid=6970909788; qb_qua=; qb_guid=818de686e29d412fa4ee9e99905ea166; Q-H5-GUID=818de686e29d412fa4ee9e99905ea166; NetType=; o_cookie=511402865; pac_uid=1_511402865; uin=o0511402865; ptisp=cnc; pgv_si=s3027380224; p_uin=o0511402865; traceid=a5329de24a; pgv_info=ssid=s9247410675; [email protected]; pt4_token=RnDTPQBuS*GfBBan7f7Kr1zwMrATa5NLuxnm*PATgNc_; p_skey=la4BrvIKFjioohQ0VaokxhOb4aZTXeIoOiZNaiDMGss_}
    req = requests.post(url,data,headers=headers,verify=False )
    return req.json()

def timestamp_to_str(timestamp=None,format=%Y-%m-%d %H:%M:%S):
    ‘‘‘时间戳转格式化好的时间,如果没有传时间戳,就获取当前的格式化时间‘‘‘
    if timestamp:
        time_tuple = time.localtime(timestamp) #把时间戳转成时间元组
        result = time.strftime(format,time_tuple) #把时间元组转成格式化好的时间
        return result
    else:
        return time.strftime(format)


def covert_gender(g):
    if g==0:
        return 
    elif g==1:
        return 
    return 未知

def filterData(data):
    result = []#存放每个用户的信息
    mem_data = data.get(mems)
    titles = [uin,join_time,card,g,nick]
    for mem in mem_data:
        d = {}
        for t in titles:
            val = mem.get(t)
            if t == join_time:  # 转换加入时间
                val = timestamp_to_str(val)
            if t == g:  # 转换性别
                val = covert_gender(val)
            d[t] = val
        result.append(d)
    return result


def writeExecl(data,excel_name):
    book = xlwt.Workbook()
    sheet = book.add_sheet(sheet1)
    titles = [uin,join_time,card,g,nick]
    for col,value in enumerate(titles,0):#写表头
        sheet.write(0,col,value)
    for row,mem in enumerate(data,1):
        for col,value in enumerate(titles,0):   #因为lie 是固定的,所以直接使用表头当key就可以了。
            val = mem.get(value)
            sheet.write(row,col,val) #写内容
    book.save(excel_name)
    print(excel写入文件)



def  saveRedis(data,key):
    host = XXXXX
    passwd = XXXXX
    r = redis.Redis(host=host, password=passwd, db=15, decode_responses=True)  # 0-15
    p = r.pipeline()
    for d in data:
        p.hset(key,d.get(uin),json.dumps(d,ensure_ascii=False))
    p.execute()
    print(redis写入完成!)

def downLoadPic(data):
    url = http://q4.qlogo.cn/g?b=qq&nk=%s&s=140
    if not os.path.exists(pics):
        os.mkdir(pics)
    for d in data:
        uid = d.get(uin)
        content = requests.get(url%uid).content
        with open(pics/%s.jpg%d.get(nick),wb) as fw:
            fw.write(content)
    print(图片下载完成)

def sendmMail(file_name):
    user = [email protected]
    passwd = XXXXX  # 授权码
    smtp_host = XXXXX
    mail = yagmail.SMTP(user=user, password=passwd, host=smtp_host)  # 连上邮箱
    mail.send(to=[[email protected], [email protected], [email protected]],
              cc=[[email protected], [email protected], [email protected]],
              subject=qq群信息,
              contents=明天上班,
              attachments=[file_name]
              )
    print(邮件发送完成!)

def main():
    data = getData()
    new_data = filterData(data)
    writeExecl(new_data,szz.xls)
    saveRedis(new_data,szz_stu)
    downLoadPic(new_data)
    sendmMail(szz.xls)
    print(done!)

 

以上是关于Python-获取QQ群成员信息并存入excel及redis并发送邮件的主要内容,如果未能解决你的问题,请参考以下文章

测试基于Python将某网前10条信息获取并实现自动发送QQ群

python获取群成员信息

PHP获取QQ群成员QQ号码

用python做些有意思的事——分析QQ聊天记录(补充)

如何禁止群成员在qq群共享上传文件?

个人微信Hook-开发SDK-C#Demo