requests和发邮件

使用requests之前需要先安装requests模块,安装命令如下:

pip install requests

下面是requests用法介绍

import requests
# url='http://api.nnzhp.cn/api/user/stu_info'
# d = {'stu_name':'矿泉水2'}
# req = requests.get(url,d)
#
# print(req.json()) #字典
# print(req.text) #字符串

# url='http://api.nnzhp.cn/api/user/login'
# data = {'username':'niuhanyang','passwd':'aA123456'}
# req = requests.post(url,data)
# print(req.json())

# url='http://api.nnzhp.cn/api/user/gold_add'
# data = {'stu_id':2,'gold':111111}
# cookie = {'niuhanyang':'5dab62839d51c0799c6ddb1476d6a511','xxx':'xxx'}
# cookie2 = {'cookie':'RK=JQZpwBp1by; ptcz=6c30e26a9ed6be93d3de9e4c4aca3e55650cf99fcffa64729bd1d58a5fb209d9; pgv_pvi=779236352; pgv_pvid=6970909788; qb_qua=; qb_guid=818de686e29d412fa4ee9e99905ea166; Q-H5-GUID=818de686e29d412fa4ee9e99905ea166; NetType=; o_cookie=511402865; pac_uid=1_511402865; uin=o0511402865; ptisp=cnc; pgv_si=s3027380224; skey=@gfJBoFjZ4; p_uin=o0511402865; pt4_token=WFIECK07PiBaZz-SLJfuHiRBNMzM9ktkgImEdNSchgg_; p_skey=QgOGMs9jcRBKhd0z6ha3Tk0UhrUz8nQ3A2fJ9pkxsAE_; traceid=a5329de24a'}
#
# # req = requests.post(url,data,cookies=cookie)
# req = requests.post(url,data,headers=cookie2)
# print(req.text)

#入参是json的
# data = {
#     "name": "矿泉水33333",
#     "grade": "双子座",
#     "phone": "12123624321",
#     "sex": "未知",
#     "addr": "天通苑",
#     "age": 38
# }
#
# url = 'http://api.nnzhp.cn/api/user/add_stu'
# req = requests.post(url,json=data)
# print(req.text)

# url='http://api.nnzhp.cn/api/file/file_upload'
# data = {'file':open('上周作业.py','rb')}
# req = requests.post(url,files=data)
# print(req.text)


# url = 'http://aliuwmp3.changba.com/userdata/userwork/12107482.mp3'
url='https://aliimg.changba.com/cache/photo/18189396_640_640.jpg'
req = requests.get(url)
with open('aqmm.jpg','wb') as fw:
    fw.write(req.content)

需要注意:
req.content
req.text
req.json()
返回的内容不同

使用yagmail之前需要先安装yagmail模块,安装命令如下:

pip install yagmail

下面是requests用法介绍

import yagmail

#邮箱的账号密码、用的是什么邮箱、163/腾讯/sina
#给谁发邮件、抄送给谁
#邮件的主题、邮件的正文、附件

user = 'test@163.com'
passwd = 'hxxx' #授权码

smtp_host ='smtp.163.com'

# smtp_host ='smtp.qq.com'

mail = yagmail.SMTP(user=user,password=passwd,host=smtp_host) #连上邮箱
mail.send(to=['xx@qq.com','111@qq.com','123@qq.com'],
          cc=['asdf@qq.com','asdf@163.com','bsdf@qq.com'],
          subject='明天好好在家休息',
          contents='明天多吃点,吃的饱饱的,在家睡个懒觉',
          attachments=['app_myuser.xls','CJ_test.xls']
          )

收件人和抄送人都可以使用列表,这样可以同时发送给多个邮箱。

posted @ 2019-05-14 14:39  狂爷  阅读(431)  评论(0编辑  收藏  举报