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"
}]