python中通过itchat导出所有微信好友信息到execl表格

本文介绍如何使用Python的itchat和xlwt模块来抓取并导出微信好友的基本信息至Excel文件,包括微信名、备注名、所在城市等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过itchat模块查询微信的好友信息
再通过xlwt模块将数据解析后写入到execl表格中
注意:
1、qr_file 这个变量是存储execl表格的路径
2、微信需要能登录网页版,不能登录网页版无法登录
3、无法获取微信号,只能获取到基本信息

# -*- coding: utf-8 -*-
# author: Hermant_Tang
import itchat
import xlwt
import os


qr_file = r'D:\\test\\wechat'
work_book = xlwt.Workbook(encoding='utf-8')
seet = work_book.add_sheet('微信好友信息', 20)
itchat.auto_login(hotReload=True)
file_name = '微信好友信息.xls'
def create_execl():
    a1 = ['City', 'PYQuanPin', 'RemarkPYInitial', 'Province', 'RemarkName', 'PYInitial', 'Signature', 'NickName',
          'RemarkPYQuanPin', 'HeadImgUrl', 'Sex', 'UserName']
    a2 = ['所属市', '好友微信名拼音', '好友备注拼音简称大写', '所属省', '备注', '微信名拼音大写', '个性签名', '微信名', '备注名拼音', '好友头像url', '性别', 'UserName']
    for i in itchat.search_friends():
        l = dict(zip(a1, a2)).get(i)
        if l == 'UserName':
            seet.write(0, 0, l)
        elif l == '备注':
            seet.write(0, 2, l)
        elif l == '微信名':
            seet.write(0, 1, l)
        elif l == '备注名拼音':
            seet.write(0, 9, l)
        elif l == '好友微信名拼音':
            seet.write(0, 7, l)
        elif l == '好友备注拼音简称大写':
            seet.write(0, 7, l)
        elif l == '微信名拼音大写':
            seet.write(0, 10, l)
        elif l == '所属市':
            seet.write(0, 5, l)
        elif l == '所属省':
            seet.write(0, 4, l)
        elif l == '个性签名':
            seet.write(0, 6, l)
        elif l == '好友头像url':
            seet.write(0, 11, l)
        elif l == '性别':
            seet.write(0, 3, l)

    work_book.save(os.path.join(qr_file, file_name))

def insert_execl():
    count = 1
    for friend_info in itchat.get_friends():
        for l in friend_info:
            info = friend_info[l]
            if l == 'UserName':
                seet.write(count, 0, info)
            elif l == 'RemarkName':
                seet.write(count, 2, info)
            elif l == 'NickName':
                seet.write(count, 1, info)
            elif l == 'RemarkPYQuanPin':
                seet.write(count, 9, info)
            elif l == 'PYQuanPin':
                seet.write(count, 7, info)
            elif l == 'RemarkPYInitial':
                seet.write(count, 7, info)
            elif l == 'PYInitial':
                seet.write(count, 10, info)
            elif l == 'City':
                seet.write(count, 5, info)
            elif l == 'Province':
                seet.write(count, 4, info)
            elif l == 'Signature':
                seet.write(count, 6, info)
            elif l == 'HeadImgUrl':
                seet.write(count, 11, info)
            elif l == 'Sex':
                if info == 1:
                    seet.write(count, 3, '男')
                elif info == 2:
                    seet.write(count, 3, '女')
                else:
                    seet.write(count, 3, '未知')
        count += 1


if __name__ == '__main__':
    create_execl()
    insert_execl()
    work_book.save(os.path.join(qr_file, file_name))
    print('数据导出完毕!!!!')
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值