以下是一个使用Python编写的提醒快递到达指定地点的代码,它可以通过调用API获取快递的物流信息,并在快递到达指定地点时发送邮件进行提醒。
```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。
点击空白处退出提示












评论