生活随笔
收集整理的這篇文章主要介紹了
定时给ta讲笑话python3.x
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
看到一篇文章——定時(shí)給ta講笑話,挺有趣的,http://www.codeceo.com/article/python-talk-funny.html
python3代碼如下:
# -*- coding: utf-8 -*-
'''
Created on 2016年5月5日
@author: mandala
'''
import urllib, json,sys,smtplib
from urllib.request import *
from email.mime.text import MIMEText#reload(sys)
#sys.setdefaultencoding('utf-8')#避免中文編碼問(wèn)題mail_host="smtp.139.com" #設(shè)置服務(wù)器
mail_user="***@139.com" #用戶名
mail_pass="***" #口令
mailto_list=['**@139.com'] #郵件接受者:手機(jī)號(hào)+@139.comdef send_mail(to_list,part1,sub,content): #to_list:收件人;sub:主題;content:郵件內(nèi)容;me=part1+"<"+mail_user+">" #hello#me=part1msg = MIMEText(content,_subtype='plain',_charset='utf-8')#創(chuàng)建一個(gè)實(shí)例,這里設(shè)置為純文字格式郵件編碼utf8msg['Subject'] = sub #設(shè)置主題#print(sub)#print(me)msg['From'] = me #設(shè)置發(fā)件人msg['To'] = ";".join(to_list) try: s = smtplib.SMTP() #實(shí)例化 s.connect(mail_host) #連接smtp服務(wù)器s.login(mail_user,mail_pass) #登陸服務(wù)器s.sendmail(me, to_list, msg.as_string()) #發(fā)送郵件s.close() return True except Exception as e: print('a'+str(e)) return Falseif __name__ == '__main__': appkey = "e2376cfbe3b27dff923ed61698839a67"url = 'http://apis.baidu.com/showapi_open_bus/showapi_joke/joke_text?page=1'req = urllib.request.Request(url)req.add_header("apikey", appkey)resp = urllib.request.urlopen(req)content = resp.read()if(content):json_result = json.loads(content.decode())content_list = json_result['showapi_res_body']['contentlist']minlen = 10000for item in content_list:if len(item['text'])<minlen:first_title = item['title']first_text = item['text']minlen = len(item['text'])print('title:'+first_title)print('content:'+first_text)length = len(first_text)part1 = 'guessWho'part2 = first_titlepart3 = first_textprint(part1,"+",part2,"+",part3)if send_mail(mailto_list,part1,part2,part3): print("send msg succeed")else: print("send msg failed")else:print("get joke error")
總結(jié)
以上是生活随笔為你收集整理的定时给ta讲笑话python3.x的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。