python解析jsonp格式数据

解决方法:

最近在调用一些api时发现返回的是jsonp格式的数据,这些数据的格式基本是这样:

callback&&callback(json_data)

因为json数据中没有括号,所以我们可以使用正则表达式json_data提取出来。

代码:

import re
def loads_jsonp(_jsonp):
    try:
        return json.loads(re.match(".*?({.*}).*",_jsonp,re.S).group(1))
    except:
        raise ValueError('Invalid Input')

例子:

这是我调腾讯地图api得到的返回数据:

text = r'qq.maps._svcb3.search_service_0 && qq.maps._svcb3.search_service_0(\n{\n    "info":\n\t{\n        "type":  9,\n\t    "error": 0,\n\t"request_id": "042091151007625e22d6786216b5c537f3a4a7dd4ae2",\n        "time":  0,\n\t\t"query":  "兰州",\n\t\t"what_query": "null",\n        "hint": 0,\n        "querytype":0,\n\t\t"qcnum": 0,\n\t\t"qctype": -1\n    },\n\t"detail":\n\t{\n\t\t"city":\n\t\t{\n\t\t\t"ccode": 2947,\n\t\t\t"acode": "620100",\n\t\t\t"cname": "兰州市",\n\t\t\t"ctype": 2, \n\t\t\t"level": 11,\n\t\t\t"pointx": "103.834170",\n\t\t\t"pointy": "36.061380",\n\t\t\t"geotype": 0,\n\t\t\t"bus": 1,\n\t\t\t"path": [{"acode":620100,"ccode":2947,"cname":"兰州市"},{"acode":620000,"ccode":2946,"cname":"甘肃省"},{"acode":0,"ccode":1,"cname":"中国"}]\n\t\t},\n         \n\t\t"area":\n\t\t{\n\t\t\t"acode": 620100,\n\t\t\t"ccode": 2947,\n\t\t\t"cname": "兰州市",\n            "ctype": 2,\n            "level": 11,\n            "pointx": "103.834170",\n            "pointy": "36.061380"\n\t\t},\n\t\t"sample": {"bus":{"dest":{"pointx":"103.872911","uid":"61ec5a109ef709bdd8c4fc26","pointy":"36.046134","poitype":1,"query":"华邦女子广场"},"start":{"pointx":"103.816301","uid":"290c3540fe8cad6988fad427","pointy":"36.065754","poitype":1,"query":"白塔山公园"}},"drive":{"dest":{"pointx":"103.872911","uid":"61ec5a109ef709bdd8c4fc26","pointy":"36.046134","poitype":1,"query":"华邦女子广场"},"start":{"pointx":"103.816301","uid":"290c3540fe8cad6988fad427","pointy":"36.065754","poitype":1,"query":"白塔山公园"}}},\n\t\t"hints": [],\n        "weather": "",\n\t\t"brief":{"area":"","link":"http://baike.soso.com/v136313.htm","content":"兰州市位于黄河上游水能富集区,河川径流地表水资源总量384亿立方米,地下水总量9.6亿立方米。以兰州为中心的黄河上游干流段可建25座大中型水电站,总装机容量可达1500万千瓦,现已建成的刘家峡、八盘峡、盐锅峡、大峡水电站与邻近地区的其它水电站构成我国最大的水力发电中心之一。","title":"兰州市"},"pictures":["http://pic.baike.soso.com/p/20090709/20090709113818-619897644.jpg"],"children":[{"ccode":"620102","cname":"城关区"},{"ccode":"620103","cname":"七里河区"},{"ccode":"620104","cname":"西固区"},{"ccode":"620105","cname":"安宁区"},{"ccode":"620111","cname":"红古区"},{"ccode":"620121","cname":"永登县"},{"ccode":"620122","cname":"皋兰县"},{"ccode":"620123","cname":"榆中县"}],"parent":{"acode": "620000", "ccode": "2946", "cname": "甘肃省"},\n        "backlink":\n        {\n            "type": 2,\n            "acode": 0,\n            "ccode": 1,\n            "cname": "中国",\n            "query": "null"\n        }\n   }\n\n})'

解析得到了json格式的数据:

{'detail': {'area': {'acode': 620100,
   'ccode': 2947,
   'cname': '兰州市',
   'ctype': 2,
   'level': 11,
   'pointx': '103.834170',
   'pointy': '36.061380'},
  'backlink': {'acode': 0,
   'ccode': 1,
   'cname': '中国',
   'query': 'null',
   'type': 2},
  'brief': {'area': '',
   'content': '兰州市位于黄河上游水能富集区,河川径流地表水资源总量384亿立方米,地下水总量9.6亿立方米。以兰州为中心的黄河上游干流段可建25座大中型水电站,总装机容量可达1500万千瓦,现已建成的刘家峡、八盘峡、盐锅峡、大峡水电站与邻近地区的其它水电站构成我国最大的水力发电中心之一。',
   'link': 'http://baike.soso.com/v136313.htm',
   'title': '兰州市'},
  'children': [{'ccode': '620102', 'cname': '城关区'},
   {'ccode': '620103', 'cname': '七里河区'},
   {'ccode': '620104', 'cname': '西固区'},
   {'ccode': '620105', 'cname': '安宁区'},
   {'ccode': '620111', 'cname': '红古区'},
   {'ccode': '620121', 'cname': '永登县'},
   {'ccode': '620122', 'cname': '皋兰县'},
   {'ccode': '620123', 'cname': '榆中县'}],
  'city': {'acode': '620100',
   'bus': 1,
   'ccode': 2947,
   'cname': '兰州市',
   'ctype': 2,
   'geotype': 0,
   'level': 11,
   'path': [{'acode': 620100, 'ccode': 2947, 'cname': '兰州市'},
    {'acode': 620000, 'ccode': 2946, 'cname': '甘肃省'},
    {'acode': 0, 'ccode': 1, 'cname': '中国'}],
   'pointx': '103.834170',
   'pointy': '36.061380'},
  'hints': [],
  'parent': {'acode': '620000', 'ccode': '2946', 'cname': '甘肃省'},
  'pictures': ['http://pic.baike.soso.com/p/20090709/20090709113818-619897644.jpg'],
  'sample': {'bus': {'dest': {'pointx': '103.872911',
     'pointy': '36.046134',
     'poitype': 1,
     'query': '华邦女子广场',
     'uid': '61ec5a109ef709bdd8c4fc26'},
    'start': {'pointx': '103.816301',
     'pointy': '36.065754',
     'poitype': 1,
     'query': '白塔山公园',
     'uid': '290c3540fe8cad6988fad427'}},
   'drive': {'dest': {'pointx': '103.872911',
     'pointy': '36.046134',
     'poitype': 1,
     'query': '华邦女子广场',
     'uid': '61ec5a109ef709bdd8c4fc26'},
    'start': {'pointx': '103.816301',
     'pointy': '36.065754',
     'poitype': 1,
     'query': '白塔山公园',
     'uid': '290c3540fe8cad6988fad427'}}},
  'weather': ''},
 'info': {'error': 0,
  'hint': 0,
  'qcnum': 0,
  'qctype': -1,
  'query': '兰州',
  'querytype': 0,
  'request_id': '042091151007625e22d6786216b5c537f3a4a7dd4ae2',
  'time': 0,
  'type': 9,
  'what_query': 'null'}}

你可能感兴趣的:(爬虫,数据可视化)