weixin_46415501 2020-02-27 11:29 采纳率: 66.7%
浏览 425
已采纳

请问这个该怎么处理?求解

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

  • 写回答

3条回答 默认 最新

  • LitterTwo 2020-02-28 09:36
    关注
    import requests
    import smtplib
    import schedule
    import time
    from email.mime.multipart import MIMEMultipart 
    from email.utils import parseaddr,formataddr
    from bs4 import BeautifulSoup
    from email.mime.text import MIMEText
    from email.header import Header
    
    account = input('请输入你的邮箱:xxxxxxxxxxxxxxxxxxxx')
    password = input('请输入你的密码:xxxxxxxxxxxxxxxx')
    recvier = input('请输入你的邮箱:xxxxxxxxxxxxxxxxxxx')
    
    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):
        subject = '今日天气预报'
        message = MIMEMultipart('alternative') 
        #下面的主题,发件人,收件人,日期是显示在邮件页面上的。
        message['From'] = formataddr(['recvier', account])
        message['To'] = recvier
        message['Subject'] = Header(subject, 'utf-8')#编码方式
        #构造文字内容  
        content= tem + weather
        content_plain = MIMEText(content, 'plain', 'utf-8') 
        message.attach(content_plain) 
        mailhost='smtp.163.com'
    
        wangyimail = smtplib.SMTP(mailhost, 25)
        wangyimail.starttls()
        wangyimail.login(account, password)
        try:
            wangyimail.sendmail(account, [recvier], message.as_string())
            print ('邮件发送成功')
        except:
            print ('邮件发送失败')
        wangyimail.quit()
    def job():
        print('开始一次任务')
        tem,weather = weather_spider()
        send_email(tem,weather)
        print('任务完成')
    
    schedule.every().day.at("23:07").do(job)
    while True:
        schedule.run_pending()
        time.sleep(1)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器