diff --git a/exercises/1901100296/d11/main.py b/exercises/1901100296/d11/main.py index c20ddc306..386d364be 100644 --- a/exercises/1901100296/d11/main.py +++ b/exercises/1901100296/d11/main.py @@ -1,17 +1,41 @@ from mymodule import stats_word import getpass import yagmail +import requests +import logging +from pyquery import PyQuery -def postemail(content,title): +# 获取网页内容 +def get_content(url): + # 请求网页返回内容 + response = requests.get(url) + # 提取微信公众号正⽂ + document = PyQuery(response.text) + content = document('#js_content').text() + return content +# 发邮件 +def post_email(content,title): sender = input('输⼊发件⼈邮箱:') password = getpass.getpass('输⼊发件⼈邮箱密码(可复制粘贴):') recipients = input('输⼊收件⼈邮箱:') - yag = yagmail.SMTP(sender,password) + # yag = yagmail.SMTP(sender,password) + # yag = yagmail.SMTP(sender,password,'smtp.163.com') + yag = yagmail.SMTP(sender,password,'smtp.qq.com') yag.send(recipients,title,content) +def main(): + logging.basicConfig(format='file:%(filename)s|line:%(lineno)d|message:%(message)s',level=logging.DEBUG) + try: + url='https://mp.weixin.qq.com/s/pLmuGoc4bZrMNl7MSoWgiA' + content = stats_word.stats_text_cn(get_content(url),100) + # content = '测试!!' + # print('统计结果:',content) + # logging.info('%s %s',type(content),str(content)) + post_email(content,'1901100296 自学训练营学习20群DAY11') + logging.info('已发送,请注意查收!') + except Exception as e: + logging.exception(e) + if __name__ == '__main__': - url='https://mp.weixin.qq.com/s/pLmuGoc4bZrMNl7MSoWgiA' - content = stats_word.stats_text_cn(stats_word.getcontent(url),100) - # print('统计结果:',content) - postemail(content,'统计结果') + main() diff --git a/exercises/1901100296/d11/mymodule/stats_word.py b/exercises/1901100296/d11/mymodule/stats_word.py index d3ee7fc9c..849b4b092 100644 --- a/exercises/1901100296/d11/mymodule/stats_word.py +++ b/exercises/1901100296/d11/mymodule/stats_word.py @@ -1,19 +1,7 @@ import collections import jieba -import requests -import yagmail -import json -from pyquery import PyQuery - -# 获取网页内容 -def getcontent(url): - # 请求网页返回内容 - response = requests.get(url) - # 提取微信公众号正⽂ - document = PyQuery(response.text) - content = document('#js_content').text() - return content +import json # 封装统计汉字词频的函数 def stats_text_cn(text,count): @@ -28,6 +16,9 @@ def stats_text_cn(text,count): words.append(word) # 使用标准库的Counter统计词和词频数,返回前count位的数据 cnt_words = collections.Counter(words).most_common(count) + + # res_words = str(cnt_words) + res_words='' for r in cnt_words: a,b = r diff --git a/exercises/1901100296/d12/main.py b/exercises/1901100296/d12/main.py index 52fc0b98d..d0b27bbc2 100644 --- a/exercises/1901100296/d12/main.py +++ b/exercises/1901100296/d12/main.py @@ -2,45 +2,27 @@ import getpass import yagmail from wxpy import * -# import wxpy -def postemail(content,title): - sender = input('输⼊发件⼈邮箱:') - password = getpass.getpass('输⼊发件⼈邮箱密码(可复制粘贴):') - recipients = input('输⼊收件⼈邮箱:') - yag = yagmail.SMTP(sender,password) - yag.send(recipients,title,content) +logging.basicConfig(format='file:%(filename)s|line:%(lineno)d|message:%(message:%(message)s',level=logging.DEBUG) +def main(): + # 初始化机器人,扫码登录 + bot = Bot() + # 找到好友和群聊 + my_friends = bot.friends() + # family_group = bot.groups() + # 监听好友信息,自动响应分享类型的消息 + @bot.register(my_friends,SHARING) + def handler(msg): + try: + logging.info('sharing url=%s',msg.url) + result = stats_word.stats_text_cn(stats_word.getcontent(msg.url),100) + msg.reply(str(result)) + except Exception as e: + logging.exception(e) + embed() +if __name__ == '__main__': + main() -# 初始化机器人,扫码登录 -bot = Bot() -# 找到好友和群聊 -my_friend = bot.friends().search('老运')[0] -# family_group = bot.groups().search('家人')[0] - -# 发送消息给好友 -my_friend.send('hello!') -# my_friend.send_image('1.jpg') -# 自动响应消息 -# 打印消息 -@bot.register() -def just_print(msg): - print(msg) -# 取得消息内容的URL -@bot.register() -def get_url(msg): - if msg.type == SHARING: - content = stats_word.stats_text_cn(stats_word.getcontent(msg.url),100) - # 回复好友消息 - @bot.register(my_friend) - def reply_my_friend(): - return '处理结果:{}'.format(content) - -embed() -# if __name__ == '__main__': - # url='https://mp.weixin.qq.com/s/pLmuGoc4bZrMNl7MSoWgiA' - # content = stats_word.stats_text_cn(stats_word.getcontent(url),100) - # print('统计结果:',content) - # postemail(content,'统计结果') diff --git a/exercises/1901100296/d12/mymodule/stats_word.py b/exercises/1901100296/d12/mymodule/stats_word.py index d3ee7fc9c..f6ee982b2 100644 --- a/exercises/1901100296/d12/mymodule/stats_word.py +++ b/exercises/1901100296/d12/mymodule/stats_word.py @@ -28,11 +28,6 @@ def stats_text_cn(text,count): words.append(word) # 使用标准库的Counter统计词和词频数,返回前count位的数据 cnt_words = collections.Counter(words).most_common(count) - res_words='' - for r in cnt_words: - a,b = r - w = a + ':' +str(b) - # print(w) - res_words = res_words + '\n' + w - - return res_words + + return cnt_words + diff --git a/exercises/1901100296/d13/SimHei.ttf b/exercises/1901100296/d13/SimHei.ttf new file mode 100644 index 000000000..36f58fbd8 Binary files /dev/null and b/exercises/1901100296/d13/SimHei.ttf differ diff --git a/exercises/1901100296/d13/main.py b/exercises/1901100296/d13/main.py new file mode 100644 index 000000000..c620a3201 --- /dev/null +++ b/exercises/1901100296/d13/main.py @@ -0,0 +1,52 @@ +from mymodule import stats_word +import getpass +import yagmail +from wxpy import * +import matplotlib.pyplot as plt +import matplotlib +import numpy as np +from os import path + +logging.basicConfig(format='file:%(filename)s|line:%(lineno)d|message:%(message:%(message)s',level=logging.DEBUG) + +file_path = path.join(path.dirname(path.abspath(__file__)),'SimHei.ttf') + +# 将统计结果画成条形图并返回条形图的保存路径 +def stats_barplot(list_data): + zhfont1 = matplotlib.font_manager.FontProperties(fname=file_path) + + words = [x[0] for x in list_data ] + frequence = [x[1] for x in list_data] + + fig,ax = plt.subplots() + ax.barh(words,frequence) + ylabels = ax.get_yticklabels() + plt.setp(ylabels,FontProperties=zhfont1) + ax.set_xlabel('词频',FontProperties=zhfont1) + ax.set_ylabel('词语',FontProperties=zhfont1) + ax.set_title('文章中出现次数在前10的词语',FontProperties=zhfont1) + png_path = path.join(path.dirname(path.abspath(__file__)),'stats_words_barplot.png') + fig.savefig(png_path,transparen=False,dpi=80,bbox_inches="tight") + # plt.show() + return png_path + +def main(): + # 初始化机器人,扫码登录 + bot = Bot() + # 找到好友和群聊 + my_friends = bot.friends() + # family_group = bot.groups() + + # 监听好友信息,自动响应分享类型的消息 + @bot.register(my_friends,SHARING) + def handler(msg): + try: + logging.info('sharing url=%s',msg.url) + msg.reply(stats_barplot(result)) + except Exception as e: + logging.exception(e) + embed() + +if __name__ == '__main__': + main() + diff --git a/exercises/1901100296/d13/mymodule/stats_word.py b/exercises/1901100296/d13/mymodule/stats_word.py new file mode 100644 index 000000000..54e8ace51 --- /dev/null +++ b/exercises/1901100296/d13/mymodule/stats_word.py @@ -0,0 +1,31 @@ +import collections +import jieba +import requests +import yagmail +import json +from pyquery import PyQuery + +# 获取网页内容 +def getcontent(url): + # 请求网页返回内容 + response = requests.get(url) + # 提取微信公众号正⽂ + document = PyQuery(response.text) + content = document('#js_content').text() + return content + + +# 封装统计汉字词频的函数 +def stats_text_cn(text,count): + if type(text)!=str: + raise ValueError('输入类型 %s,参数必须是 str 类型'%type(text)) + # 使用jieba分词 + seg_words = jieba.cut(text) + # 长度大于等于2的词 + words = [] + for word in seg_words: + if len(word) >= 2: + words.append(word) + # 使用标准库的Counter统计词和词频数,返回前count位的数据 + cnt_words = collections.Counter(words).most_common(count) + return cnt_words