每天定时给你的女朋友发送睡前小故事其一

2023-11-25 22:20

本文主要是介绍每天定时给你的女朋友发送睡前小故事其一,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这是一个爬取故事,每天定时发送的案例,发送给谁呢?可以发送给自己的女朋友,什么?不可能有女朋友?那就好好练习一下代码,学会之后说不准哪天就用到了,不对吗?

代码主要分成两部分,第一部分是爬取故事网站,然后把故事的连接保存到文件中。代码部分如下

    def request_html(self,url,title_url):response = requests.get(url=url,headers=self.headers).content.decode('utf-8')tree = etree.HTML(response)title = tree.xpath('.//div[@id="Mhead2_0"]//dd//a/text()')href = tree.xpath('.//div[@id="Mhead2_0"]//dd//a/@href')href = ["http://www.tom61.com"+hre for hre in href]for t,h in zip(title,href):dict_story = {}dict_story[t] = htitle_url.append(dict_story)print(title_url)# json_str = json.dumps(title_url,ensure_ascii=False)with open('故事.json','w',encoding='utf-8') as f:json.dump(title_url,f,ensure_ascii=False)#爬取小故事def crawlStory(self,endpage):title_url = []url = 'http://www.tom61.com/ertongwenxue/shuiqiangushi/index.html'self.request_html(url,title_url)for i in range(2, endpage):url = 'http://www.tom61.com/ertongwenxue/shuiqiangushi/index_%s.html' % (i)self.request_html(url,title_url)

第二部分则是获取url,然后解析,把故事发送到她的邮箱中的代码,代码如下

    #发送小故事到邮箱当中def sendStory(self):with open(r'F:\reptile\第二周\第五天\故事.json', 'r', encoding='utf-8') as f:content = f.read()story = json.loads(content)story_url = random.choice(story)for k, v in story_url.items():name = kurl = vresponse = requests.get(url=url, headers=self.headers).content.decode('utf-8')tree = etree.HTML(response)content_list = tree.xpath('.//div[@class="t_news_txt"]//text()')content = "\n".join(content_list)print(content)self.sendemail(name,content,'66666666@qq.com')# 发送邮箱的函数def sendemail(self,subject,content,recver):subject = subject  # 邮件主题content = content  # 邮件内容sender = "88888888@qq.com"  # 发送者的邮箱recver = recver  # 接收者的邮箱,可以是一个也可以是多个,多个的话要使用recver.split(",\n")转换格式password = "xxxxxxxxxxxxxx"  # 授权码部分message = MIMEText(content, "plain", "utf-8")  # 设置格式,加入邮件内容message["Subject"] = subjectmessage["To"] = recvermessage["From"] = sendertry:smtp = smtplib.SMTP_SSL("smtp.qq.com", 465)  # 使用的服务器和端口,可以是qq的也可以是163的smtp.login(sender, password)  # 登录邮箱smtp.sendmail(sender, recver.split(",\n"), message.as_string())  # 发送邮件smtp.close()  # 关闭服务器print('发送成功')except:print('出现异常')

完整的代码如下

import requests
from fake_useragent import UserAgent
from lxml import etree
import json
import smtplib  # 登陆邮件服务器,进行邮件发送
from email.mime.text import MIMEText  # 负责构建邮件格式
import randomclass Story():def __init__(self):self.ua = UserAgent()self.useragent = self.ua.randomself.headers = {'User-Agent': self.useragent}#爬取保存小故事到文件中def request_html(self,url,title_url):response = requests.get(url=url,headers=self.headers).content.decode('utf-8')tree = etree.HTML(response)title = tree.xpath('.//div[@id="Mhead2_0"]//dd//a/text()')href = tree.xpath('.//div[@id="Mhead2_0"]//dd//a/@href')href = ["http://www.tom61.com"+hre for hre in href]for t,h in zip(title,href):dict_story = {}dict_story[t] = htitle_url.append(dict_story)print(title_url)# json_str = json.dumps(title_url,ensure_ascii=False)with open('故事.json','w',encoding='utf-8') as f:json.dump(title_url,f,ensure_ascii=False)#爬取小故事def crawlStory(self,endpage):title_url = []url = 'http://www.tom61.com/ertongwenxue/shuiqiangushi/index.html'self.request_html(url,title_url)for i in range(2, endpage):url = 'http://www.tom61.com/ertongwenxue/shuiqiangushi/index_%s.html' % (i)self.request_html(url,title_url)#发送小故事到邮箱当中def sendStory(self):with open(r'F:\reptile\第二周\第五天\故事.json', 'r', encoding='utf-8') as f:content = f.read()story = json.loads(content)story_url = random.choice(story)for k, v in story_url.items():name = kurl = vresponse = requests.get(url=url, headers=self.headers).content.decode('utf-8')tree = etree.HTML(response)content_list = tree.xpath('.//div[@class="t_news_txt"]//text()')content = "\n".join(content_list)print(content)self.sendemail(name,content,'66666666@qq.com')# 发送邮箱的函数def sendemail(self,subject,content,recver):subject = subject  # 邮件主题content = content  # 邮件内容sender = "88888888@qq.com"  # 发送者的邮箱recver = recver  # 接收者的邮箱,可以是一个也可以是多个,多个的话要使用recver.split(",\n")转换格式password = "xxxxxxxxxxxxxx"  # 授权码部分message = MIMEText(content, "plain", "utf-8")  # 设置格式,加入邮件内容message["Subject"] = subjectmessage["To"] = recvermessage["From"] = sendertry:smtp = smtplib.SMTP_SSL("smtp.qq.com", 465)  # 使用的服务器和端口,可以是qq的也可以是163的smtp.login(sender, password)  # 登录邮箱smtp.sendmail(sender, recver.split(",\n"), message.as_string())  # 发送邮件smtp.close()  # 关闭服务器print('发送成功')except:print('出现异常')if __name__ == '__main__':story = Story()# story.crawlStory(3)story.sendStory()

 

这篇关于每天定时给你的女朋友发送睡前小故事其一的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/424711

相关文章

java实现延迟/超时/定时问题

《java实现延迟/超时/定时问题》:本文主要介绍java实现延迟/超时/定时问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java实现延迟/超时/定时java 每间隔5秒执行一次,一共执行5次然后结束scheduleAtFixedRate 和 schedu

Spring定时任务只执行一次的原因分析与解决方案

《Spring定时任务只执行一次的原因分析与解决方案》在使用Spring的@Scheduled定时任务时,你是否遇到过任务只执行一次,后续不再触发的情况?这种情况可能由多种原因导致,如未启用调度、线程... 目录1. 问题背景2. Spring定时任务的基本用法3. 为什么定时任务只执行一次?3.1 未启用

Spring Boot 集成 Quartz 使用Cron 表达式实现定时任务

《SpringBoot集成Quartz使用Cron表达式实现定时任务》本文介绍了如何在SpringBoot项目中集成Quartz并使用Cron表达式进行任务调度,通过添加Quartz依赖、创... 目录前言1. 添加 Quartz 依赖2. 创建 Quartz 任务3. 配置 Quartz 任务调度4. 启

Spring Boot中定时任务Cron表达式的终极指南最佳实践记录

《SpringBoot中定时任务Cron表达式的终极指南最佳实践记录》本文详细介绍了SpringBoot中定时任务的实现方法,特别是Cron表达式的使用技巧和高级用法,从基础语法到复杂场景,从快速启... 目录一、Cron表达式基础1.1 Cron表达式结构1.2 核心语法规则二、Spring Boot中定

如何使用C#串口通讯实现数据的发送和接收

《如何使用C#串口通讯实现数据的发送和接收》本文详细介绍了如何使用C#实现基于串口通讯的数据发送和接收,通过SerialPort类,我们可以轻松实现串口通讯,并结合事件机制实现数据的传递和处理,感兴趣... 目录1. 概述2. 关键技术点2.1 SerialPort类2.2 异步接收数据2.3 数据解析2.

Windows server服务器使用blat命令行发送邮件

《Windowsserver服务器使用blat命令行发送邮件》在linux平台的命令行下可以使用mail命令来发送邮件,windows平台没有内置的命令,但可以使用开源的blat,其官方主页为ht... 目录下载blatBAT命令行示例备注总结在linux平台的命令行下可以使用mail命令来发送邮件,Win

使用Java发送邮件到QQ邮箱的完整指南

《使用Java发送邮件到QQ邮箱的完整指南》在现代软件开发中,邮件发送功能是一个常见的需求,无论是用户注册验证、密码重置,还是系统通知,邮件都是一种重要的通信方式,本文将详细介绍如何使用Java编写程... 目录引言1. 准备工作1.1 获取QQ邮箱的SMTP授权码1.2 添加JavaMail依赖2. 实现

Qt实现发送HTTP请求的示例详解

《Qt实现发送HTTP请求的示例详解》这篇文章主要为大家详细介绍了如何通过Qt实现发送HTTP请求,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1、添加network模块2、包含改头文件3、创建网络访问管理器4、创建接口5、创建网络请求对象6、创建一个回复对

Spring Boot 整合 ShedLock 处理定时任务重复执行的问题小结

《SpringBoot整合ShedLock处理定时任务重复执行的问题小结》ShedLock是解决分布式系统中定时任务重复执行问题的Java库,通过在数据库中加锁,确保只有一个节点在指定时间执行... 目录前言什么是 ShedLock?ShedLock 的工作原理:定时任务重复执行China编程的问题使用 Shed

Java中Springboot集成Kafka实现消息发送和接收功能

《Java中Springboot集成Kafka实现消息发送和接收功能》Kafka是一个高吞吐量的分布式发布-订阅消息系统,主要用于处理大规模数据流,它由生产者、消费者、主题、分区和代理等组件构成,Ka... 目录一、Kafka 简介二、Kafka 功能三、POM依赖四、配置文件五、生产者六、消费者一、Kaf