定時傳送睡前小故事 python

2021-09-23 22:37:47 字數 2076 閱讀 2424

原文: 

# -*- coding: utf-8 -*-

import requests

from bs4 import beautifulsoup

import smtplib

from email.mime.text import mimetext

import random

def gethtmltext(url,headers):

try:

r=requests.get(url,headers=headers,timeout=30)

r.raise_for_status()

print(r.text)

return r.text

except:

return "爬取失敗"

def parsehtml(namelist,urllist,html):

url=''

soup=beautifulsoup(html,'html.parser')

t=soup.find('dl',attrs=)

#print(t)

i=t.find_all('a')

#print(i)

for link in i:

def parsehtml2(html):

text=

soup=beautifulsoup(html,'html.parser')

t=soup.find('div',class_='t_news_txt')

for i in t.findall('p'):

print(text)

return "\n".join(text)

def sendemail(url,headers):

msg_from='' #傳送方郵箱

passwd='' #填入傳送方郵箱的授權碼

receivers=[''] #收件人郵箱

subject='今日份的小故事' #主題

html=gethtmltext(url,headers)

content=parsehtml2(html) #正文

msg = mimetext(content)

msg['subject'] = subject

msg['from'] = msg_from

msg['to'] = ','.join(receivers)

try:

s=smtplib.smtp_ssl("smtp.qq.com",465) #郵件伺服器及埠號

s.login(msg_from, passwd)

s.sendmail(msg_from, msg['to'].split(','), msg.as_string())

print("傳送成功")

except:

print("傳送失敗")

finally:

s.quit()

def main():

}urllist=

namelist=

for i in range(1,11):

if i==1:

url='ertongwenxue/shuiqiangushi/index.html'

else:

url='ertongwenxue/shuiqiangushi/index_'+str(i)+'.html'

print (url+'\n')

html=gethtmltext(url,headers)

parsehtml(namelist,urllist,html)

print("爬取鏈結完成")

sendemail(random.choice(urllist),headers)

if __name__=='__main__':

main()

python實現定時傳送郵件

import schedule import time import smtplib from smtplib import smtp ssl from email.mime.text import mimetext from email.mime.multipart import mimemult...

python實現定時傳送系列

1.傳送郵件實現 2.定時任務實現 3.定時傳送郵件實現 詳細源 見 參考 廖雪峰部落格 smtp傳送郵件 定時任務schedule模組 1.傳送郵件實現 使用smtplib和email兩個模組實現郵件傳送,如下 1 傳送郵件功能 2from email import encoders 3from ...

python定時傳送郵件 爬蟲監控 爬蟲定時啟動

from email.mime.text import mimetext from smtplib import smtp defsend email self,text 傳送郵件 param text return 定義發件人 msg from xx qq.com 授權碼 password ndi...