目標(biāo)
通過調(diào)用移動(dòng)之家提供的版本號(hào)接口獲取當(dāng)前線上環(huán)境 iOS 平臺(tái)最新版本的版本號(hào)乔煞。
第一步:獲取 http 請(qǐng)求結(jié)果
#!/usr/bin/python
# -*- coding: utf-8 -*-
# 使用 requests 庫
import requests
response = requests.get('http://home.mobile.******')
versionInfo = response.text
參考資料:python中http請(qǐng)求方法庫匯總:https://www.cnblogs.com/landhu/p/5104628.html
第二步:解析 json
# 引用 json 庫用于解析 json 對(duì)象
import json
# versionInfo 是接口返回的 json 格式數(shù)據(jù)
# json.loads 將已編碼的 JSON 字符串解碼為 Python 對(duì)象
versionInfoPython = json.loads(versionInfo)
# print versionInfoPython
# 從字典里取 data 數(shù)組
dataList = versionInfoPython.get('data')
# print dataList
# 滿足條件:platformJson 有 iOS 以及 status 為發(fā)布狀態(tài)谬擦, 獲得滿足條件時(shí)相應(yīng)的 version 值
# 取前20個(gè)遍歷
firstDataList = dataList[0:20]
versionList = []
for x in firstDataList:
? ? # print x['platformJson']
? ? # 將 platformJson 對(duì)象解析為 platform逞带, 由接口返回結(jié)果知 platformPython 是一個(gè)列表(數(shù)組)
? ? platformPython = json.loads(x['platformJson'])
? ? if len(platformPython) > 1 and x['status'] == 1 :
? ? ? ? versionList.append(x['version'])
# print versionList
print versionList[0]?
參考資料:使用Python解析JSON詳解 https://www.cnblogs.com/wangyayun/p/6699184.html?utm_source=tuicool&utm_medium=referral
完整代碼
#!/usr/bin/python
# -*- coding: utf-8 -*-
# 引用 json 庫用于解析 json 對(duì)象
import json
# 使用 requests 庫
import requests
response = requests.get('http://home.mobile.******')
versionInfo = response.text
# versionInfo 是接口返回的 json 格式數(shù)據(jù)
# json.loads 將已編碼的 JSON 字符串解碼為 Python 對(duì)象
versionInfoPython = json.loads(versionInfo)
# print versionInfoPython
# 從字典里取 data 數(shù)組
dataList = versionInfoPython.get('data')
# print dataList
# 取前20個(gè)遍歷
firstDataList = dataList[0:20]
versionList = []
for x in firstDataList:
? ? # print x['platformJson']
? ? # 將 platformJson 對(duì)象解析為 platform现恼, 由接口返回結(jié)果知 platformPython 是一個(gè)列表(數(shù)組)
? ? platformPython = json.loads(x['platformJson'])
? ? # 當(dāng) platformPython 數(shù)組大于1時(shí)凛澎,一定包含 ios
? ? # status : 1 已發(fā)布,2 開發(fā)中适掰,4 內(nèi)灰中
? ? if len(platformPython) > 1 and x['status'] == 1 :
? ? ? ? versionList.append(x['version'])
# print versionList
print versionList[0]?