无意中看到罗罗攀老师的文章,发现这个itchat这个库真的太爽了。
记录一下使用的itchat爬取微信头像的代码:
```
importitchat
importmath
importPIL.ImageasImage
importos
itchat.auto_login()
friends = itchat.get_friends(update=True)[0:]
user = friends[0]["UserName"]
num =0
foriinfriends:
img = itchat.get_head_img(userName=i["UserName"])
fileImage =open('E:\python\image'+"/"+str(num) +".jpg",'wb')
fileImage.write(img)
fileImage.close()
num +=1
ls = os.listdir('E:\python\image')
each_size =int(math.sqrt(float(640*640)/len(ls)))
lines =int(640/each_size)
image = Image.new('RGB', (640,640))
x =0
y =0
foriinrange(0,len(ls)+1):
try:
img = Image.open('E:\python\image'+"/"+str(i) +".jpg")
exceptIOError:
print("Error")
else:
img = img.resize((each_size, each_size), Image.ANTIALIAS)
image.paste(img, (x * each_size, y * each_size))
x +=1
ifx == lines:
x =0
y +=1
image.save('E:\python\image'+"/"+"all.jpg")
itchat.send_image('E:\python\image'+"/"+"all.jpg",'filehelper')
```
先用pip下载itchat库,命令为:pip install itchat
之后用python执行:python index.py
扫描登录微信