Python 网络数据采集 学习笔记 第五章 Email (2019/7/1)
在这一节中,在输入书(2016)中 Page79 的代码,你或许会遇到:
1.[WinError 10061] 由于目标计算机积极拒绝,无法连接。
2.TypeError: memoryview: a bytes-like object is required, not ‘MIMEText’
3.程序运行正常,但收件箱没收到邮件.
原代码 (错误 WinError 10061)
def sendMail(subject, body):
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = "sender's QQ num@163.com"
msg['to'] = "receiver's QQ num@qq.com"
s = smtplib.SMTP('localhost')
s.send(msg)
s.quit()
修改后
def sendMail(subject, body):
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = "sender's QQ num@163.com"
msg['to'] = "receiver's QQ num@qq.com"
user = 'sender's QQ num@163.com'
# 你的邮箱密码
password = '****'
# 这里使用的是163邮箱
smtpObj = smtplib.SMTP_SSL("smtp.163.com",465)
smtpObj.login(user,password)
smtpObj.send(msg)
smtpObj.quit()
SSL是一种数据加密,身份认证和消息完整性验证机制。我们在通过SMTP传输邮件时,需要在自己邮箱-设置-账户中查看是否开启。
原代码 报错 TypeError: memoryview: a bytes-like object is required, not ‘MIMEText’
s.send(msg)
修改后
smtpObj.send(msg.as_string())
程序运行正常,但没有收到邮件
1. 中国用户在 https://isitchristmas.com 中显示是"不是" 而不是 “NO”
因此修改为:
bsOBJ = BeautifulSoup(urlopen("https://isitchristmas.com/"), "html.parser")
while (bsOBJ.find("a", {"id":"answer"}).attrs['title'] == "不是"):
print("It is not Christmas yet.")
time.sleep(3600)
bsOBJ = BeautifulSoup(urlopen("https://isitchristmas.com/"), "html.parser")
2.检查收件箱是否开启STMP服务
qq邮箱:https://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256
参考文献:
Python 网络数据采集(RYan Mitchell 2016)
SMTPAuthenticationError 535 Error(关键词:flask/bug) ( HenryQWER)