每天自动给你的那个TA发送一个小故事,是不是很浪漫啊?
让我们一起来实现吧!
首先,找个目标,诶,巧了,我这里有一个:http://www.tom61.com/ertongwenxue/shuiqiangushi/
既然目标有了,那就 盘他!(盘他前要去该网站“踩个点”)
踩点完毕就开始吧###############################
1.导入必要的模块
import smtplib
from email.mime.text import MIMEText
import requests
from lxml import etree
import time
2.获取多个故事的地址(爬之前加个伪装头)
headers={
‘User-Agent’: ‘Mozilla/5.0(Windows NT10.0;Win64;x64) AppleWebKit/537.36(KHTML,likeGecko) Chrome/74.0.3729.169Safari/537.36’
}
def url_request():
url='http://www.tom61.com/ertongwenxue/shuiqiangushi/'
response=requests.get(url,headers=headers).content.decode('utf-8')
# print(response)
tree=etree.HTML(response)
dd_list=tree.xpath('//dl[@class="txt_box"]/dd')[0:40]
url_list=[]
for dd in dd_list:
url1=dd.xpath('.//a/@href')
url_list.append(url1)
# print(url_list)
这里我是直接获取40个地址(只是真正地址的一部分,后续要拼接),然后把这些地址复制出来(整个列表哦),要用!为啥这样做呢?节约资源嘛,不用每天再去跑这个了!
3.那就开始爬故事了
def story_request(url):
response=requests.get(url,headers=headers).content.decode('utf-8')
tree=etree.HTML(response)
title=tree.xpath('//div[@class="t_news"]/h1/text()')[0]
story=tree.xpath('//div[@class="t_news_txt"]//text()')
story1='\n'.join(''.join(story).split())
print(title)
print(story1)
if name == ‘main’:
base_url='http://www.tom61.com'
url_list=[['/ertongwenxue/shuiqiangushi/2018-02-25/106432.html'], ['/ertongwenxue/shuiqiangushi/2018-02-25/106431.html'], ['/ertongwenxue/shuiqiangushi/2018-02-25/106430.html'], ['/ertongwenxue/shuiqiangushi/2018-02-25/106429.html'], ['/ertongwenxue/shuiqiangushi/2018-02-25/106428.html'], ['/ertongwenxue/shuiqiangushi/2018-02-25/106427.html'], ['/ertongwenxue/shuiqiangushi/2018-02-25/106426.html'], ['/ertongwenxue/shuiqiangushi/2018-02-25/106425.html'], ['/ertongwenxue/shuiqiangushi/2018-02-25/106424.html'], ['/ertongwenxue/shuiqiangushi/2018-02-25/106422.html'], ['/ertongwenxue/shuiqiangushi/2018-02-25/106423.html'], ['/ertongwenxue/shuiqiangushi/2018-01-31/106405.html'], ['/ertongwenxue/shuiqiangushi/2018-01-31/106404.html'], ['/ertongwenxue/shuiqiangushi/2018-01-31/106403.html'], ['/ertongwenxue/shuiqiangushi/2018-01-12/106212.html'], ['/ertongwenxue/shuiqiangushi/2018-01-12/106211.html'], ['/ertongwenxue/shuiqiangushi/2018-01-12/106210.html'], ['/ertongwenxue/shuiqiangushi/2018-01-12/106209.html'], ['/ertongwenxue/shuiqiangushi/2018-01-12/106208.html'], ['/ertongwenxue/shuiqiangushi/2017-12-19/106107.html'], ['/ertongwenxue/shuiqiangushi/2017-12-19/106106.html'], ['/ertongwenxue/shuiqiangushi/2017-12-19/106105.html'], ['/ertongwenxue/shuiqiangushi/2017-12-19/106104.html'], ['/ertongwenxue/shuiqiangushi/2017-12-19/106103.html'], ['/ertongwenxue/shuiqiangushi/2017-12-19/106102.html'], ['/ertongwenxue/shuiqiangushi/2017-12-19/106101.html'], ['/ertongwenxue/shuiqiangushi/2017-12-04/106022.html'], ['/ertongwenxue/shuiqiangushi/2017-12-04/106021.html'], ['/ertongwenxue/shuiqiangushi/2017-12-04/106020.html'], ['/ertongwenxue/shuiqiangushi/2017-12-04/106019.html'], ['/ertongwenxue/shuiqiangushi/2017-11-28/106003.html'], ['/ertongwenxue/shuiqiangushi/2017-11-28/106002.html'], ['/ertongwenxue/shuiqiangushi/2017-11-28/106001.html'], ['/ertongwenxue/shuiqiangushi/2017-11-28/106000.html'], ['/ertongwenxue/shuiqiangushi/2017-11-28/105999.html'], ['/ertongwenxue/shuiqiangushi/2017-11-28/105998.html'], ['/ertongwenxue/shuiqiangushi/2017-11-28/105997.html'], ['/ertongwenxue/shuiqiangushi/2017-11-28/105996.html'], ['/ertongwenxue/shuiqiangushi/2017-11-28/105995.html'], ['/ertongwenxue/shuiqiangushi/2017-11-28/105994.html']]
times=time.localtime()
s = time.strftime('%Y-%m-%d',times) #以时间中的日来取url_list中的url
index=int(s.split('-')[-1])
urls=base_url+url_list[index][0]
story_request(urls)
4.故事拿到了,那就该发邮件了
def mail(title,story):
send_from='xxxx@qq.com' #发送者邮箱(你的!!!)
passwd='XXXXXXXX' #密码,不是你QQ密码哦,而是刚才在邮箱里生成的授权码
send_to=['eeee@qq.com','eeea@163.com','aaa@qq.com'] #接收方,可同时向多人发送
subject=title+'---来自xxx发的小故事' #邮件主题部分内容 ,加号后面内容可自行修改
content=story
msg=MIMEText(content) #邮件正文
msg['Subject']=subject #邮件主题
msg['From']=send_from
msg['To']=','.join(send_to)
try:
s = smtplib.SMTP_SSL("smtp.qq.com",465) #邮件服务器及端口号
s.login(send_from, passwd)
s.sendmail(send_from, send_to, msg.as_string())
print ("发送成功")
except Exception as e:
print(e)
print ("发送失败")
截止到现在,功能就可实现了,至于上面函数的拼接,调用,我就不说了啊!
还有,定时执行部分请参考:
https://blog.csdn.net/jiabaoyu0001/article/details/95638295
欢迎大家留言交流