You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

IT管理员请求修复/编写批量查询苹果设备保修信息的Python代码

Hey there! As an IT admin managing hundreds of Apple devices, I totally feel your pain when a go-to tool stops working. Let’s build a reliable, batch-friendly Python solution to check Apple device warranty info—no more broken scripts or manual checks!

Prerequisites

First, make sure you have the requests library installed (it handles HTTP calls to Apple’s API):

pip install requests
Bulk Apple Warranty Check Script

Here’s a tested script that reads a list of device serial numbers, queries Apple’s official warranty endpoint, and saves results to a CSV file for easy analysis. It includes error handling for invalid serials or API timeouts:

import requests
import csv
import time

def check_apple_warranty(serial_number):
    # Apple's official warranty check endpoint (updated as of 2024)
    url = "https://checkcoverage.apple.com/us/en/"
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
    }
    data = {
        "sn": serial_number,
        "locale": "en_US"
    }
    
    try:
        # Add a small delay to avoid hitting API rate limits
        time.sleep(1)
        response = requests.post(url, headers=headers, data=data)
        response.raise_for_status()
        
        # Parse the JSON response
        warranty_data = response.json()
        
        # Extract key info (adjust fields based on your needs)
        result = {
            "Serial Number": serial_number,
            "Device Name": warranty_data.get("productDescription", "N/A"),
            "Coverage Status": warranty_data.get("coverageStatus", "N/A"),
            "Purchase Date": warranty_data.get("purchaseDate", "N/A"),
            "End of Coverage": warranty_data.get("coverageEndDate", "N/A"),
            "Status": "Success"
        }
    except requests.exceptions.RequestException as e:
        result = {
            "Serial Number": serial_number,
            "Device Name": "N/A",
            "Coverage Status": "N/A",
            "Purchase Date": "N/A",
            "End of Coverage": "N/A",
            "Status": f"Error: {str(e)}"
        }
    except ValueError:
        result = {
            "Serial Number": serial_number,
            "Device Name": "N/A",
            "Coverage Status": "N/A",
            "Purchase Date": "N/A",
            "End of Coverage": "N/A",
            "Status": "Invalid Serial Number or API Response"
        }
    return result

def batch_check_warranties(serial_file_path, output_csv_path):
    # Read serial numbers from a text file (one per line)
    with open(serial_file_path, "r") as f:
        serial_numbers = [line.strip() for line in f if line.strip()]
    
    # Prepare CSV output
    fieldnames = ["Serial Number", "Device Name", "Coverage Status", "Purchase Date", "End of Coverage", "Status"]
    with open(output_csv_path, "w", newline="", encoding="utf-8") as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
        writer.writeheader()
        
        # Process each serial number
        for serial in serial_numbers:
            print(f"Checking {serial}...")
            warranty_info = check_apple_warranty(serial)
            writer.writerow(warranty_info)
    
    print(f"Batch check complete! Results saved to {output_csv_path}")

if __name__ == "__main__":
    # Replace these paths with your actual file locations
    SERIAL_FILE = "apple_serials.txt"  # One serial number per line
    OUTPUT_CSV = "apple_warranty_results.csv"
    
    batch_check_warranties(SERIAL_FILE, OUTPUT_CSV)
How to Use This Script
  1. Prepare your serial list: Create a text file named apple_serials.txt with one Apple device serial number per line (no extra spaces).
  2. Run the script: Execute it with Python:
    python apple_warranty_check.py
    
  3. View results: Open the generated apple_warranty_results.csv in Excel or Google Sheets to see all warranty details at a glance.
Notes
  • The time.sleep(1) is important to avoid triggering Apple’s API rate limits—adjust it if needed (but 1 second per request is safe for bulk checks).
  • If Apple updates their API endpoint in the future, you may need to tweak the url or request parameters. This script uses the current official endpoint as of 2024.
  • Invalid serial numbers or network issues will be flagged in the "Status" column of the CSV.

内容的提问来源于stack exchange,提问作者Jeffin Jacob

火山引擎 最新活动