python 优雅的解析 jsonp

python

一段 jsonp 格式数据

mtopjsonpweexcb1({"api":"mtop.taobao.idle.recycle.nextspunav.get","data":{"ext":{},"items":[{"bannerFlag":false,"hasNextLevel":true,"hasReturnNextLevel":true,"hotLabel":true,"hotLabelName":"热门","subNextPage":false,"subRecycleSpuNavDOList":[{"bannerFlag":false,"hasNextLevel":false,"hasReturnNextLevel":false,"hotLabel":false,"name":"MacBook Air 13.3","spuId":506,"subNextPage":false},{"bannerFlag":false,"hasNextLevel":false,"hasReturnNextLevel":false,"hotLabel":false,"name":"苹果 Macbook Pro 13 i5五代","spuId":507,"subNextPage":false},{"bannerFlag":false,"hasNextLevel":false,"hasReturnNextLevel":false,"hotLabel":false,"name":"联想 拯救者R720","spuId":2102,"subNextPage":false},{"bannerFlag":false,"hasNextLevel":false,"hasReturnNextLevel":false,"hotLabel":false,"name":"Thinkpad E470C","spuId":2571,"subNextPage":false},{"bannerFlag":false,"hasNextLevel":false,"hasReturnNextLevel":false,"hotLabel":false,"name":"戴尔 G3 15","spuId":1703808,"subNextPage":false},{"bannerFlag":false,"hasNextLevel":false,"hasReturnNextLevel":false,"hotLabel":false,"name":"华硕 FX80","spuId":1703725,"subNextPage":false},{"bannerFlag":false,"hasNextLevel":false,"hasReturnNextLevel":false,"hotLabel":false,"name":"神舟 战神Z7M","spuId":1677,"subNextPage":false},{"bannerFlag":false,"hasNextLevel":false,"hasReturnNextLevel":false,"hotLabel":false,"name":"炫龙 毁灭者DC","spuId":72423,"subNextPage":false},{"bannerFlag":false,"hasNextLevel":false,"hasReturnNextLevel":false,"hotLabel":false,"name":"小米 笔记本Pro","spuId":1443679,"subNextPage":false},{"bannerFlag":false,"hasNextLevel":false,"hasReturnNextLevel":false,"hotLabel":false,"name":"华为 Matebook X Pro","spuId":1443531,"subNextPage":false}],"subTotalCount":10},{"bannerFlag":false,"hasNextLevel":true,"hasReturnNextLevel":true,"hotLabel":false,"id":30111,"name":"苹果","subNextPage":true,"subRecycleSpuNavDOList":[{"bannerFlag":false,"hasNextLevel":false,"hasReturnNextLevel":false,"hotLabel":false,"id":63986242,"name":"苹果 15年 13寸 MacBook Air","spuId":506,"subNextPage":false,"type":"keyProp2Id"},{"bannerFlag":false,"hasNextLevel":false,"hasReturnNextLevel":false,"hotLabel":false,"id":2891556036,"name":"苹果18年13寸MacBookAir","spuId":3439453,"subNextPage":false,"type":"keyProp2Id"},{"bannerFlag":false,"hasNextLevel":false,"hasReturnNextLevel":false,"hotLabel":false,"id":2891556038,"name":"苹果18年15寸MacBookPro","spuId":3439455,"subNextPage":false,"type":"keyProp2Id"},{"bannerFlag":false,"hasNextLevel":false,"hasReturnNextLevel":false,"hotLabel":false,"id":2891556037,"name":"苹果18年13寸MacBookPro","spuId":3439454,"subNextPage":false,"type":"keyProp2Id"},{"bannerFlag":false,"hasNextLevel":false,"hasReturnNextLevel":false,"hotLabel":false,"id":88764409,"name":"苹果 15年 11寸 MacBook Air","spuId":515,"subNextPage":false,"type":"keyProp2Id"},{"bannerFlag":false,"hasNextLevel":false,"hasReturnNextLevel":false,"hotLabel":false,"id":1930119344,"name":"苹果 14年 13寸 MacBook Pro","spuId":508,"subNextPage":false,"type":"keyProp2Id"},{"bannerFlag":false,"hasNextLevel":false,"hasReturnNextLevel":false,"hotLabel":false,"id":1930119343,"name":"苹果 15年 13寸 MacBook Pro","spuId":507,"subNextPage":false,"type":"keyProp2Id"},{"bannerFlag":false,"hasNextLevel":false,"hasReturnNextLevel":false,"hotLabel":false,"id":1930119346,"name":"苹果 15年 15寸 MacBook Pro","spuId":510,"subNextPage":false,"type":"keyProp2Id"},{"bannerFlag":false,"hasNextLevel":false,"hasReturnNextLevel":false,"hotLabel":false,"id":1930119347,"name":"苹果 14年 15寸 MacBook Pro","spuId":511,"subNextPage":false,"type":"keyProp2Id"},{"bannerFlag":false,"hasNextLevel":false,"hasReturnNextLevel":false,"hotLabel":false,"id":1979880045,"name":"苹果 16年 13寸 MacBook Pro","spuId":1327136,"subNextPage":false,"type":"keyProp2Id"},{"bannerFlag":false,"hasNextLevel":false,"hasReturnNextLevel":false,"hotLabel":false,"id":1979880044,"name":"苹果 16年 15寸 MacBook Pro","spuId":1327138,"subNextPage":false,"type":"keyProp2Id"},{"bannerFlag":false,"hasNextLevel":false,"hasReturnNextLevel":false,"hotLabel":false,"id":1979880046,"name":"苹果 17年 13寸 MacBook Pro","spuId":1327137,"subNextPage":false,"type":"keyProp2Id"},{"bannerFlag":false,"hasNextLevel":false,"hasReturnNextLevel":false,"hotLabel":false,"id":1979880043,"name":"苹果 17年 15寸 MacBook Pro","spuId":1327139,"subNextPage":false,"type":"keyProp2Id"},{"bannerFlag":false,"hasNextLevel":false,"hasReturnNextLevel":false,"hotLabel":false,"id":1979880047,"name":"苹果 17年 12寸 MacBook","spuId":1327135,"subNextPage":false,"type":"keyProp2Id"},{"bannerFlag":false,"hasNextLevel":false,"hasReturnNextLevel":false,"hotLabel":false,"id":863236658,"name":"苹果 16年 12寸 MacBook","spuId":505,"subNextPage":false,"type":"keyProp2Id"},{"bannerFlag":false,"hasNextLevel":false,"hasReturnNextLevel":false,"hotLabel":false,"id":1979880048,"name":"苹果 塑料壳 09年 13寸 MacBook","spuId":1327134,"subNextPage":false,"type":"keyProp2Id"},{"bannerFlag":false,"hasNextLevel":false,"hasReturnNextLevel":false,"hotLabel":false,"id":1930119349,"name":"MacBook Pro 15 酷睿系列","spuId":513,"subNextPage":false,"type":"keyProp2Id"},{"bannerFlag":false,"hasNextLevel":false,"hasReturnNextLevel":false,"hotLabel":false,"id":1930119348,"name":"MacBook Pro 15 酷睿i系列","spuId":512,"subNextPage":false,"type":"keyProp2Id"},{"bannerFlag":false,"hasNextLevel":false,"hasReturnNextLevel":false,"hotLabel":false,"id":1930119345,"name":"MacBook Pro 13 机械硬盘","spuId":509,"subNextPage":false,"type":"keyProp2Id"},{"bannerFlag":false,"hasNextLevel":false,"hasReturnNextLevel":false,"hotLabel":false,"id":1581598684,"name":"新款Macbook Pro 15英寸","spuId":518,"subNextPage":false,"type":"keyProp2Id"}],"subTotalCount":23,"type":"keyProp1Id"},{"bannerFlag":false,"hasNextLevel":true,"hasReturnNextLevel":false,"hotLabel":false,"id":11119,"name":"联想","subNextPage":false,"type":"keyProp1Id"},{"bannerFlag":false,"hasNextLevel":true,"hasReturnNextLevel":false,"hotLabel":false,"id":11656,"name":"华硕","subNextPage":false,"type":"keyProp1Id"},{"bannerFlag":false,"hasNextLevel":true,"hasReturnNextLevel":false,"hotLabel":false,"id":26683,"name":"戴尔","subNextPage":false,"type":"keyProp1Id"},{"bannerFlag":false,"hasNextLevel":true,"hasReturnNextLevel":false,"hotLabel":false,"id":184048021,"name":"ThinkPad","subNextPage":false,"type":"keyProp1Id"},{"bannerFlag":false,"hasNextLevel":true,"hasReturnNextLevel":false,"hotLabel":false,"id":21660,"name":"神舟","subNextPage":false,"type":"keyProp1Id"},{"bannerFlag":false,"hasNextLevel":true,"hasReturnNextLevel":false,"hotLabel":false,"id":31140,"name":"惠普","subNextPage":false,"type":"keyProp1Id"},{"bannerFlag":false,"hasNextLevel":true,"hasReturnNextLevel":false,"hotLabel":false,"id":26691,"name":"宏碁","subNextPage":false,"type":"keyProp1Id"},{"bannerFlag":false,"hasNextLevel":true,"hasReturnNextLevel":false,"hotLabel":false,"id":123574,"name":"alienware","subNextPage":false,"type":"keyProp1Id"},{"bannerFlag":false,"hasNextLevel":true,"hasReturnNextLevel":false,"hotLabel":false,"id":81156,"name":"三星","subNextPage":false,"type":"keyProp1Id"},{"bannerFlag":false,"hasNextLevel":true,"hasReturnNextLevel":false,"hotLabel":false,"id":21989,"name":"微软","subNextPage":false,"type":"keyProp1Id"},{"bannerFlag":false,"hasNextLevel":true,"hasReturnNextLevel":false,"hotLabel":false,"id":4535518,"name":"雷蛇","subNextPage":false,"type":"keyProp1Id"},{"bannerFlag":false,"hasNextLevel":true,"hasReturnNextLevel":false,"hotLabel":false,"id":676006358,"name":"炫龙","subNextPage":false,"type":"keyProp1Id"},{"bannerFlag":false,"hasNextLevel":true,"hasReturnNextLevel":false,"hotLabel":false,"id":21999,"name":"微星","subNextPage":false,"type":"keyProp1Id"},{"bannerFlag":false,"hasNextLevel":true,"hasReturnNextLevel":false,"hotLabel":false,"id":10752,"name":"索尼","subNextPage":false,"type":"keyProp1Id"},{"bannerFlag":false,"hasNextLevel":true,"hasReturnNextLevel":false,"hotLabel":false,"id":775486237,"name":"机械革命","subNextPage":false,"type":"keyProp1Id"},{"bannerFlag":false,"hasNextLevel":true,"hasReturnNextLevel":false,"hotLabel":false,"id":10745,"name":"东芝","subNextPage":false,"type":"keyProp1Id"},{"bannerFlag":false,"hasNextLevel":true,"hasReturnNextLevel":false,"hotLabel":false,"id":527976624,"name":"麦本本","subNextPage":false,"type":"keyProp1Id"},{"bannerFlag":false,"hasNextLevel":true,"hasReturnNextLevel":false,"hotLabel":false,"id":616784001,"name":"机械师","subNextPage":false,"type":"keyProp1Id"},{"bannerFlag":false,"hasNextLevel":true,"hasReturnNextLevel":false,"hotLabel":false,"id":11016,"name":"海尔","subNextPage":false,"type":"keyProp1Id"}],"needDecryptKeys":[],"nextPage":true,"serverTime":"2019-08-06 19:40:09","totalCount":28},"ret":["SUCCESS::调用成功"],"v":"1.0"})

方法

import json, re

def loads_jsonp(self, _jsonp):

"""

解析jsonp数据格式为json

:return:

"""

try:

return json.loads(re.match(".*?({.*}).*", _jsonp, re.S).group(1))

except:

raise ValueError(\'Invalid Input\')

以上是 python 优雅的解析 jsonp 的全部内容, 来源链接: utcz.com/z/389243.html

回到顶部