如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
itchat 查看手机微信中用户比例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import itchat import pandas as pd # Python抓取微信好友数量、性别、城市分布,以及将py文件打包成exe. #https://zhuanlan.zhihu.com/p/73295760 # 先登录 itchat.auto_login(hotReload=True) # 获取好友列表 friends = itchat.get_friends(update=True)[0:] #number_friends = itchat.get_friends(update=True) number_of_friends = len(friends) # 初始化计数器,有男有女,当然,有些人是不填的 male = female = other = 0 # 遍历这个列表,列表里第一位是自己,所以从"自己"之后开始计算 # 1表示男性,2女性 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:]) #好友城市分布 df_friends = pd.DataFrame(friends) City = df_friends.City City_count = City.value_counts() City_count = City_count[City_count.index != ''] # 好了,打印结果 friends_numbers = str(number_of_friends) man = (u"男性好友:%.2f%%" % (float(male) / total * 100)) women = (u"女性好友:%.2f%%" % (float(female) / total * 100)) others = (u"其他:%.2f%%" % (float(other) / total * 100)) city = City_count with open('wechat.txt', 'w') as f: f.write(friends_numbers) f.write('\n'+man) f.write(women) f.write('\n'+str(city)) |

获取文章中的美女图
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# _*_coding:utf-8_*_ from urllib.parse import urljoin import requests import re import os class GetImage(object): def __init__(self, url): self.url = url self.headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36' } self.dir_path = os.path.dirname(os.path.abspath(__file__)) self.path = self.dir_path + '/imgs' isExists = os.path.exists(self.dir_path + '/imgs') # 创建目录 if not isExists: os.makedirs(self.path) def download(self, url): try: res = requests.get(url, headers=self.headers) return res except Exception as E: print(url + '下载失败,原因:' + E) def parse(self, res): content = res.content.decode() # print(content) img_list = re.findall(r'<img.*?src="(.*?)"', content, re.S) img_list = [urljoin(self.url, url) for url in img_list] return img_list def save(self, res_img, file_name): if (file_name.endswith('jpg')) or (file_name.endswith('png')): file_name = file_name else: file_name = file_name + '.jpg' if res_img: with open(file_name, 'wb') as f: f.write(res_img.content) print(url + '下载成功') def run(self): # 下载 res = self.download(self.url) # 解析 url_list = self.parse(res) # 下载图片 for url in url_list: res_img = self.download(url) name = url.strip().split('/').pop() file_name = self.path + '/' + name # 保存 self.save(res_img, file_name) if __name__ == '__main__': url_list = ['http://www.umei.cc/meinvtupian/xingganmeinv/123828.htm'] for url in url_list: text = GetImage(url) text.run() |
词云

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# 导入扩展库 import re # 正则表达式库 import collections # 词频统计库 import numpy as np # numpy数据处理库 import jieba # 结巴分词 import wordcloud # 词云展示库 from PIL import Image # 图像处理库 import matplotlib.pyplot as plt # 图像展示库 # 读取文件 fn = open('article.txt') # 打开文件 string_data = fn.read() # 读出整个文件 fn.close() # 关闭文件 # 文本预处理 pattern = re.compile(u'\t|\n|\.|-|:|;|\)|\(|\?|"') # 定义正则表达式匹配模式 string_data = re.sub(pattern, '', string_data) # 将符合模式的字符去除 # 文本分词 seg_list_exact = jieba.cut(string_data, cut_all = False) # 精确模式分词 object_list = [] remove_words = [u'的', u',',u'和', u'是', u'随着', u'对于', u'对',u'等',u'能',u'都',u'。',u' ',u'、',u'中',u'在',u'了', u'通常',u'如果',u'我们',u'需要'] # 自定义去除词库 for word in seg_list_exact: # 循环读出每个分词 if word not in remove_words: # 如果不在去除词库中 object_list.append(word) # 分词追加到列表 # 词频统计 word_counts = collections.Counter(object_list) # 对分词做词频统计 word_counts_top10 = word_counts.most_common(10) # 获取前10最高频的词 print (word_counts_top10) # 输出检查 # 词频展示 mask = np.array(Image.open('img/love.jpg')) # 定义词频背景 wc = wordcloud.WordCloud( font_path="./font/simhei.ttf",# 设置字体格式 mask=mask, # 设置背景图 max_words=200, # 最多显示词数 max_font_size=100 # 字体最大值 ) wc.generate_from_frequencies(word_counts) # 从字典生成词云 image_colors = wordcloud.ImageColorGenerator(mask) # 从背景图建立颜色方案 wc.recolor(color_func=image_colors) # 将词云颜色设置为背景图方案 plt.imshow(wc) # 显示词云 plt.axis('off') # 关闭坐标轴 plt.show() # 显示图像 |
###