线下预约到了对我就无用了发出来给新手学习参考下python的requests,json这2个非常常用模块的使用方法. 

# !/usr/bin/env python
#-*- coding: UTF-8 -*-

import requests
import json
import re
import time
from playsound import playsound

flag = True  #True只显示可取货的店铺,Flase显示所有.


def getIphoneInfo():
    result = requests.get('https://www.apple.com.cn/shop/fulfillment-messages?pl=true&parts.0=MLHC3CH/A&location=%E6%B5%99%E6%B1%9F%20%E6%9D%AD%E5%B7%9E%20%E4%B8%8A%E5%9F%8E%E5%8C%BA')
    is_json = check_json_format(result.text)
    if is_json == True:
        dict_str = json.loads(result.text)
        storesCount = re.sub('\D', '', dict_str['body']['content']['pickupMessage']['storesCount'])
        #print('监控浙江省内店家总数:' + storesCount)
        for i in range(int(storesCount)):
            dic_data = str(dict_str['body']['content']['pickupMessage']['stores'][i]['partsAvailability']['MLHC3CH/A']['storePickupQuote'])  #取出字典中的字段值
            if flag == False:
                print(dic_data)
            else:
                if (dic_data.find('不可取货') == -1):
                    print(dic_data)
                    playsound('./Music.mp3', True)
        print('====================' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + '刷新====================')


def sleeptime(hour, min, sec):
    return hour * 3600 + min * 60 + sec


def check_json_format(raw_msg):
    #用于判断一个字符串是否符合Json格式
    if isinstance(raw_msg, str) == True:  # 首先判断变量是否为字符串
        try:
            json.loads(raw_msg, encoding = 'utf-8')
        except ValueError:
            return False
        return True
    else:
        return False


if __name__ == "__main__":
    second = sleeptime(0, 0, 0)  #每10秒循环一次
    while 1 == 1:
        getIphoneInfo()
        time.sleep(second)

直接下载: 浙江省iPhone13线下预监控python3版.rar - 蓝奏云

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐