def getSignature(): itchat.login() friends = itchat.get_friends(update=True) file = open('sign.txt', 'a', encoding='utf-8') for f in friends: signature = f["Signature"].strip().replace("emoji", "").replace("span", "").replace("class", "") rec = re.compile("1f\d+\w*|[<>/=]") signature = rec.sub("", signature) file.write(signature + "\n")
def headImg(): itchat.login() friends = itchat.get_friends(update=True) # itchat.get_head_img() 获取到头像二进制,并写入文件,保存每张头像 for count, f in enumerate(friends): # 根据userName获取头像 img = itchat.get_head_img(userName=f["UserName"]) imgFile = open("img/" + str(count) + ".jpg", "wb") imgFile.write(img) imgFile.close()
def parse_signature(): itchat.login() siglist = [] friedns = itchat.get_friends(update=True)[1:] for i in friedns: signature = i["Signature"].strip().replace("span", "").replace("class", "").replace("emoji", "") rep = re.compile("1f\d+\w*|[<>/=]") signature = rep.sub("", signature) siglist.append(signature) text = "".join(siglist) with io.open('text.txt', 'a', encoding='utf-8') as f: wordlist = jieba.cut(text, cut_all=True) word_space_split = " ".join(wordlist) f.write(word_space_split) f.close()
def getSex(): itchat.login() friends = itchat.get_friends(update=True) sex = dict() for f in friends: if f["Sex"] == 1: # 男 sex["man"] = sex.get("man", 0) + 1 elif f["Sex"] == 2: # 女 sex["women"] = sex.get("women", 0) + 1 else: # 未知 sex["unknown"] = sex.get("unknown", 0) + 1 # 柱状图展示 for i, key in enumerate(sex): plt.bar(key, sex[key]) plt.savefig("getsex.png") #保存图片 plt.ion() plt.pause(5) plt.close() #图片显示5s,之后关闭
def parse_friedns(): itchat.login() text = dict() friedns = itchat.get_friends(update=True)[0:] print(friedns) male = "male" female = "female" other = "other" for i in friedns[1:]: sex = i['Sex'] if sex == 1: text[male] = text.get(male, 0) + 1 elif sex == 2: text[female] = text.get(female, 0) + 1 else: text[other] = text.get(other, 0) + 1 total = len(friedns[1:]) print("男性好友: %.2f%%" % (float(text[male]) / total * 100) + "\n" + "女性好友: %.2f%%" % (float(text[female]) / total * 100) + "\n" + "不明性别好友: %.2f%%" % (float(text[other]) / total * 100)) draw(text)
#!/usr/bin/env python3 #coding utf-8 import itchat itchat.login() friends=itchat.get_friends(update=True)[0:] print(friends) male = female = other = 0 for i in friends[1:]: sex = i['Sex'] if sex == 1: male+=1 elif sex == 2: female+=1 else: other+=1 total = len(friends[1:]) print("男性朋友:%.2f%%" % (float(male)/total*100) + "\n" + "女性朋友:%.2f%%" % (float(female)/total*100) + "\n" + "其他:%.2f%%" % (float(other)/total*100) + "\n" )
import itchat import re import jieba import matplotlib.pyplot as plt from wordcloud import WordCloud, ImageColorGenerator import PIL.Image as Image import os import numpy as np itchat.login() itchat.send(u'hello', 'filehelper') tList = [] # get friend list friends = itchat.get_friends(update=True)[0:] for i in friends[1:]: #print(i) name = i["NickName"] signature = i["Signature"].strip().replace("span", "").replace( "class", "").replace("emoji", "") rep = re.compile("1f\d.+") signature = rep.sub("", signature) tList.append(signature) #print(name, signature) text = u"".join(tList) wordlist_jieba = jieba.cut(text, cut_all=True) wl_space_split = " ".join(wordlist_jieba)
def main(): itchat.login() friends = itchat.get_friends() for friend in friends: print(friend)
#coding:utf-8 import re import itchat import numpy as np from os import path import PIL.Image as Image from pandas import DataFrame import matplotlib.pyplot as plt from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator itchat.login() #爬取自己好友相关信息,返回一个json文件 friends = itchat.get_friends(update=True)[0:] #------------统计各性别人数------------# male = female = other = 0 #初始化 #下标0是自己 for i in friends[1:]: sex = i['Sex'] if sex == 1: male += 1 elif sex == 2: female += 1 else: other += 1 total = len(friends[1:]) #计算朋友总数 #------------打印个性别人数------------# print('男性好友:%.2f%%' % (float(male) / total * 100)) print('女性好友:%.2f%%' % (float(female) / total * 100)) print('未知性别好友:%.2f%%' % (float(other) / total * 100))
# coding=utf-8 # -*- coding utf-8 -*- # python 2.7 import itchat import json # itchat.auto_login() # data = '1111111' # data = data.decode('utf8') # test = '朴学峰' # test = test.decode('utf8') # tt = itchat.search_friends(test) # tt = json.dumps(tt, ensure_ascii=False) # mm = json.loads(tt) # for i in mm: # while 1: # itchat.send(data, toUserName=i['UserName']) result = itchat.login() print type(result)
def login(): itchat.login() itchat.send(u'你好', 'filehelper') pass
def get_gzh_articles(session, subscription_file='wechat_subscriptions.txt', timedel=2, sort_by_time=True, max_trial=5, sync_account=False, add_account=None): '''Get updated articles for your WeChat subscriptions. Parameters -------------------- sync_account: bool, default False. sync subscriptions to your wechat account timedel: int. days to filter most recent articles sort_by_time: bool. whether sort articles by time or not max_trial: int. max times of trial if there are accounts with no articles returned add_account: list or None. update subscription list with given account(s) Returns -------------------- article_list: articles are held in dicts account_nohit: accounts didn't return any article ''' subscription_file='wechat_subscriptions.txt' if sync_account: itchat.login() mps = itchat.get_mps(update=True) mps_name = [mp['NickName'] for mp in mps] mps_name = '\n'.join(mps_name) with open(subscription_file, 'w', encoding='utf-8-sig') as f: f.write(mps_name) itchat.logout() with open(subscription_file, 'r', encoding='utf-8-sig') as f: # handle BOM on Windows accounts = [account.strip() for account in f.readlines() if account != '\n'] # add accounts if add_account is not None: for new_account in add_account: if new_account not in accounts: accounts.append(new_account) with open(subscription_file, 'a', encoding='utf8') as f: f.write(new_account + '\n') else: print('Got duplicate account: ' + new_account) # fetch articles articles, epoch = [], 0 while len(accounts) > 0 and epoch < max_trial: for account in accounts: subscription, article_list, exit_flag = search_gzh(session, account, max_num=None) if exit_flag: break if article_list != list(): accounts.remove(account) for article_ in article_list: article_['Account name'] = subscription articles.extend(article_list) time.sleep(8) epoch += 1 if epoch < max_trial - 1: time.sleep(60) if articles == list(): return [], accounts # filter time if len(articles) > 0: timestamp = int((datetime.datetime.now()-datetime.timedelta(days=timedel)).timestamp()) articles = [article for article in articles if article['update_time'] > timestamp] # Final formatting # sort by time if sort_by_time: articles = sorted(articles, key=lambda x: x['update_time'], reverse=True) articles_ = [] for article in articles: time_str = datetime.datetime.fromtimestamp(article['update_time']).ctime() articles_.extend([{'Title': article['title'], 'Abstract': article['digest'], 'Account name': article['Account name'], 'Publication time': time_str, 'url': article['link']}]) return articles_, accounts
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> import itchat █ >>> itchat.login() Getting uuid of QR code. Downloading QR code. Please scan the QR code to log in. Please press confirm on your phone. Loading the contact, this may take a little while. Login successfully as Diablo >>> friends = itchat.get_friends(update = True )[0:] male= >>> male=female = other =0 >>> for i in friends[1:]: sex = i["Sex"] if sex ==1: male +=1 elif sex ==2: female += 1 else: other +=1 total = len(friends[1:]) SyntaxError: invalid syntax >>> for i in friends[1:]: sex = i["Sex"] if sex ==1: male +=1 elif sex ==2: female += 1 else:
def login(): itchat.login() friends = itchat.get_friends(update=True)[0:] return friends
# -*- coding: utf-8 -*- import itchat as wx import jieba import wordcloud import re import matplotlib.pyplot as plt from wordcloud import WordCloud import PIL.Image as Image wx.login() friends = wx.get_friends(update=True)[0:] signatures = [] for i in friends: signature = i["Signature"].strip().replace("emoji", "").replace( "span", "").replace("class", "").replace("</>", "").replace('\n', "") regex = re.compile(r"1f\d.+") signature = regex.sub("", signature) signatures.append(signature) text = "".join(signatures) word_list = jieba.cut(text, cut_all=True) # splice words word_split = " ".join(word_list)