import requests
import smtplib
import schedule
import time
from bs4 import BeautifulSoup
from email.mime.text import MIMEText
from email.header import Header
account = input('请输入你的邮箱:xxxxxxxxxxxxxxxx@163.com')
password = input('请输入你的密码:xxxxxxxxxxxxx')
receiver = input('请输入收件人的邮箱:xxxxxxxxxxxxx@163.com')
def weather_spider():
headers={'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36'}
url='http://www.weather.com.cn/weather1d/101290101.shtml'
res=requests.get(url,headers=headers)
res.encoding='utf-8'
soup=BeautifulSoup(res.text,'html.parser')
tem1= soup.find(class_='tem')
weather1= soup.find(class_='wea')
tem=tem1.text
weather=weather1.text
return tem,weather
def send_email(tem,weather):
mailhost='smtp.163.com'
wangyimail = smtplib.SMTP()
wangyimail.connect(mailhost,25)
wangyimail.login(account,password)
content= tem + weather
message = MIMEText(content, 'plain', 'utf-8')
subject = '今日天气预报'
message['Subject'] = Header(subject, 'utf-8')
try:
wangyimail.sendmail(account, receiver, message.as_string())
print ('邮件发送成功')
except:
print ('邮件发送失败')
wangyimail.quit()
def job():
print('开始一次任务')
tem,weather = weather_spider()
send_email(tem,weather)
print('任务完成')
schedule.every().day.at("10:15").do(job)
while True:
schedule.run_pending()
time.sleep(1)
................................................................................................................................................................................................................................................................
Traceback (most recent call last):
File "D:\hello-world.py\tq-yj.py", line 50, in
schedule.run_pending()
File "D:\下载\lib\site-packages\schedule-0.6.0-py3.9.egg\schedule__init__.py", line 563, in run_pending
default_scheduler.run_pending()
File "D:\下载\lib\site-packages\schedule-0.6.0-py3.9.egg\schedule__init__.py", line 94, in run_pending
self._run_job(job)
File "D:\下载\lib\site-packages\schedule-0.6.0-py3.9.egg\schedule__init__.py", line 147, in run_job
ret = job.run()
File "D:\下载\lib\site-packages\schedule-0.6.0-py3.9.egg\schedule\_init__.py", line 466, in run
ret = self.job_func()
File "D:\hello-world.py\tq-yj.py", line 44, in job
send_email(tem,weather)
File "D:\hello-world.py\tq-yj.py", line 29, in send_email
wangyimail.login(account,password)
File "D:\下载\lib\smtplib.py", line 723, in login
(code, resp) = self.auth(
File "D:\下载\lib\smtplib.py", line 635, in auth
(code, resp) = self.docmd("AUTH", mechanism + " " + response)
TypeError: can only concatenate str (not "bytes") to str