python处理Apple设备信息

在Apple官网上传设备信息文档时,有时因为数据太大,手动去处理的话会浪费大量的时间,所以为iOS开发小伙伴们写了一个数据处理的python脚本,可以帮大家一键处理大量数据并自动生成可直接上传的TXT文件,代码如下:

# --*--coding:utf-8 --*--
import os,pypinyin,sys,importlib,json
importlib.reload(sys)

def pinyin(word):
    s = ''
    for i in pypinyin.pinyin(word, style=pypinyin.NORMAL):
        s += ''.join(i)
    return s

def check_contain_chinese(check_str):
  for ch in check_str.encode('utf-8').decode('utf-8'):
    if u'\u4e00' <= ch <= u'\u9fff':
      return True
  return False

def write_data():
    file = "./devices.txt"
    mode = "a" if os.path.exists(file) else "w+"
    with open(file, mode, encoding='utf-8') as file:
        file.truncate(0)
        file.write("Device ID   Device Name Device Platform" + "\n")
        data = json.load(open('./apple.txt', 'r',encoding='utf-8'))
        for i in range(len(data['data'])):
            Device_Name = data["data"][i]['attributes']['name'].replace(' ', '').replace('‘', '').\
            replace('’', '').replace('(','').replace(')','').replace('(','').replace(')','').\
            replace('“','').replace('”','').replace('ʀ','R')
            if "外部客户" in Device_Name:
                continue
            elif check_contain_chinese(Device_Name):
                Device_Name = pinyin(Device_Name)
            Device_ID = data["data"][i]['attributes']['udid']
            space = "   "
            file.write(Device_ID + space + Device_Name + space + 'ios' + "\n")
write_data()

使用方法如下:

1.下载python3并配置python3环境(MacOS系统自带了python2.7,但是2和3语法不同,我们这里使用的是python3),
2.创建一个XX.py文件(名字可以自己起),复制粘贴代码至py文件内,保存
3.在Apple官网用开发者工具抓取设备接口返回的信息,如 "data" : [ {
"type" : "devices",
"id" : "QA24MV8TJ7",
"attributes" : {
"addedDate" : "2020-12-01T02:24:04.000+0000",
"serialNumber" : null,
"description" : null,
"platform" : "IOS",
"name" : "April",
"deviceClass" : "IPHONE",
"model" : "iPhone 6",
"udid" : "9db9f287ce27fc7feb3c8fda7a091eea9ffb6fb2",
"platformName" : "iOS",
"responseId" : "d81b3a33-9af4-4ae2-897f-feb1109958f2",
"status" : "ENABLED",
"devicePlatformLabel" : "iPhone"
4.在python脚本所在目录创建一个apple.txt文件,把接口返回的所有信息粘贴进此文件并保存。
5.在终端执行命令:pip3 install pypinyin
6.在终端运行命令:python3 xxx.py 此命令会运行python脚本,会自动生成一个device.txt的文件,此文件就是你要上传的文件啦。
======================================================================<

讨论数量: 2

good

3年前

感谢杨同学的分享,另外分享一下如何抓取苹果设备列表:在Safari上,按command + option + i,然后出现控制台后,点击清除按钮,然后刷新网页,会出现一系列网络回调,点击devices请求(绿色箭头),然后复制所有信息存储为apple.txt文件,与以上的python脚本放在同一个层级,然后执行phthon3 x.py就可以在同层级生成devices txt文件,然后在苹果开发正网站上传的时候一键上传就行。
注:脚本里面的筛选条件是专门针对公司网站设计的,如果用于其他地方,需要调整下处理条件。
file

3年前

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!