码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-05-24 19:31:19      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:com   work   car   一个   ascii   sub   save   not   imp   

‘‘‘
需求:
  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; skey=@q9o11HspH; 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 = XXXXX@163.com
    passwd = XXXXX  # 授权码
    smtp_host = XXXXX
    mail = yagmail.SMTP(user=user, password=passwd, host=smtp_host)  # 连上邮箱
    mail.send(to=[511402865@qq.com, XXXXX@qq.com, XXXXX@qq.com],
              cc=[gaomq@fadada.com, XXXXX@163.com, XXXXX@qq.com],
              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并发送邮件

标签:com   work   car   一个   ascii   sub   save   not   imp   

原文地址:https://www.cnblogs.com/huoxn/p/10919503.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!