python定时发送邮件

E:\reptile\day10\邮件发送\具体故事.py

#-*-coding:utf-8-*-
#-*-coding:utf-8-*-
import requests,json
from  fake_useragent import  UserAgent
from  lxml  import  etree
import random
ua=UserAgent()
def  story_html(url,headers):#列表页
    response=requests.get(url=url,headers=headers).content.decode('utf-8')
    tree=etree.HTML(response)
    contents=tree.xpath('.//div[@class="t_news_txt"]//p/text()')
    content='\n'.join(contents)
    print(content)
    sendemail(name, content)
#第二步,发送邮件
import  smtplib
from  email.mime.text import MIMEText
def  sendemail(name,content):
    msgfrom='3303236612@qq.com'#发送方邮箱:
    password='qetlyluxzqpndbjh'#授权码
    receiver='3303236612@qq.com'#收件人邮箱
    subject=name#主体
    msg=MIMEText(content,'plain','utf-8')#发送内容
    msg['Subject']=subject
    msg['Form'] = msgfrom
    msg['To'] =receiver
    try:
        smtp=smtplib.SMTP()
        smtp.connect('smtp.qq.com')
        smtp.login(msgfrom,password)
        smtp.sendmail(msgfrom,msg['To'],msg.as_string())
        print('发送成功')
    except:
        print('发送失败')
        pass
    pass
if __name__ == '__main__':
    with  open(r'E:\reptile\day10\邮件发送\故事.json', 'r', encoding='utf-8')as  fq:
        story_href = fq.read()
    headers = {'User-Agent': ua.random}
    story = json.loads(story_href)
    story_url = random.choice(story)
    print(story_url)
    for  k,v in story_url.items():
        name=k
        url=v
        story_html(url,headers)



E:\reptile\day10\邮件发送\故事.json部分数据

[
  {
    "1.古今战役之日德兰海战": "http://www.tom61.com/ertongwenxue/zhanzhenggushi/2017-04-01/92835.html"
  },
  {
    "2.古今战役之苏俄内战和外国武装干涉": "http://www.tom61.com/ertongwenxue/zhanzhenggushi/2017-04-01/92834.html"
  },
  {
    "3.古今战役之俄波战争": "http://www.tom61.com/ertongwenxue/zhanzhenggushi/2017-04-01/92833.html"
  },
  {
    "4.古今战役之埃塞俄比亚抗意战争": "http://www.tom61.com/ertongwenxue/zhanzhenggushi/2017-04-01/92832.html"
  },
  {
    "5.古今战役之西班牙内战": "http://www.tom61.com/ertongwenxue/zhanzhenggushi/2017-04-01/92831.html"
  },
  {
    "6.古今战役之日本侵华战争": "http://www.tom61.com/ertongwenxue/zhanzhenggushi/2017-04-01/92830.html"
  }]
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值