python通过手机号批量搜索微信好友并保存头像到文件夹
时间: 2023-09-17 09:10:04 浏览: 129
要通过手机号批量搜索微信好友并保存头像到文件夹,可以使用itchat库来实现。首先需要安装itchat库,然后编写以下代码:
```python
import itchat
import os
# 登录微信
itchat.auto_login()
# 手机号列表,用于搜索微信好友
phone_list = ['手机号1', '手机号2', '手机号3']
# 循环搜索好友
for phone in phone_list:
friends = itchat.search_friends(phone=phone)
for friend in friends:
# 保存好友头像到文件夹
img = itchat.get_head_img(userName=friend['UserName'])
img_path = os.path.join('头像文件夹', f"{friend['NickName']}.jpg")
with open(img_path, 'wb') as f:
f.write(img)
# 退出微信登录
itchat.logout()
```
其中,`phone_list`参数为手机号列表,用于搜索微信好友。执行以上代码后,会循环搜索每个手机号对应的微信好友,并保存好友的头像到指定的文件夹中。可以根据需要对搜索结果进行处理。注意,如果要使用该功能,需要先获取好友的手机号,并在好友的个人信息中填写手机号。
相关问题
python通过excel表格中的手机号批量搜索陌生微信号并保存头像到文件夹
要通过Excel表格中的手机号批量搜索陌生微信号并保存头像到文件夹,可以使用openpyxl和itchat库来实现。首先需要安装openpyxl和itchat库,然后编写以下代码:
```python
import openpyxl
import itchat
import os
# 登录微信
itchat.auto_login()
# 打开Excel文件并获取工作表
wb = openpyxl.load_workbook('手机号.xlsx')
ws = wb.active
# 循环搜索好友
for row in ws.iter_rows(min_row=2):
phone = row[0].value
friends = itchat.search_friends(phone=phone)
for friend in friends:
# 判断是否为已添加好友
if friend['Friend']:
continue
# 保存好友头像到文件夹
img = itchat.get_head_img(userName=friend['UserName'])
img_path = os.path.join('头像文件夹', f"{friend['NickName']}.jpg")
with open(img_path, 'wb') as f:
f.write(img)
# 退出微信登录
itchat.logout()
```
其中,`手机号.xlsx`为包含手机号的Excel文件名,第一列为手机号。执行以上代码后,会循环搜索Excel表格中每个手机号对应的陌生微信号,并保存好友的头像到指定的文件夹中。可以根据需要对搜索结果进行处理。注意,如果要使用该功能,需要先获取好友的手机号,并在好友的个人信息中填写手机号。
python批量搜索微信好友
可以使用itchat库来实现Python批量搜索微信好友。首先需要安装itchat库,然后编写以下代码:
```python
import itchat
itchat.auto_login()
friends = itchat.search_friends(name='好友昵称')
for friend in friends:
print(friend['NickName'], friend['RemarkName'], friend['UserName'])
```
其中,`name`参数为好友的昵称,可以通过昵称进行搜索。执行以上代码后,会输出好友的昵称、备注和用户名。可以根据需要对搜索结果进行处理。