点击空白处退出提示
作品详情
```python
import requests
import json
import time
import smtplib
from email.mime.text import MIMEText
# 定义API地址和参数
api_url = 'https://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx'
api_key = '填写您的API Key'
api_secret = '填写您的API Secret'
express_company = '填写您要查询的快递公司代码'
express_number = '填写您要查询的快递单号'
destination = '填写您要提醒的快递到达地点'
# 定义邮件发送参数
mail_host = '填写您的SMTP服务器地址'
mail_port = 465
mail_user = '填写您的发件人邮箱'
mail_password = '填写您的发件人邮箱密码'
mail_sender = '填写您的发件人邮箱'
mail_receiver = '填写您的收件人邮箱'
# 定义查询快递信息的函数
def query_express_state():
params = {
'RequestType': '1002',
'EBusinessID': api_key,
'RequestData': json.dumps({
'OrderCode': '',
'ShipperCode': express_company,
'LogisticCode': express_number
}),
'DataType': 2
}
headers = {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
}
response = requests.post(api_url, data=params, headers=headers)
result = json.loads(response.text)
if result['Success']:
state = result['State']
if state == '3':
for trace in result['Traces']:
if destination in trace['AcceptStation']:
return True
return False
else:
return None
# 定义发送邮件的函数
def send_email(subject, message):
mail_body = MIMEText(message, 'plain', 'utf-8')
mail_body['From'] = mail_sender
mail_body['To'] = mail_receiver
mail_body['Subject'] = subject
smtp = smtplib.SMTP_SSL(mail_host, mail_port)
smtp.login(mail_user, mail_password)
smtp.sendmail(mail_sender, mail_receiver, mail_body.as_string())
smtp.quit()
# 主程序
while True:
result = query_express_state()
if result is None:
print('查询快递信息失败')
break
elif result:
print('快递已到达指定地点,发送提醒邮件')
send_email('您的快递已到达指定地点', '您的快递已经到达指定地点,请尽快取件')
break
else:
print('快递还未到达指定地点,继续查询')
time.sleep(60) # 每隔60秒查询一次快递状态
```
在使用本代码前,请确保已安装必要的Python库:requests、smtplib等。并且在使用API前,请先到快递鸟官网注册并获取API Key和API Secret。
声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态
评论