1
_ntes_stocksearch_callback([{"type":"SZ","symbol":"000001","name":"a","spell":"PAYH"},{"type":"SH","symbol":"000001","name":"b","spell":"SZZS"},{"type":"FN","symbol":"000001","name":"c","spell":"HXCZHH"}])

如何编写正则表达式模式以获得如下结果:

{"type":"SZ","symbol":"000001","name":"a","spell":"PAYH"},
{"type":"SH","symbol":"000001","name":"b","spell":"SZZS"},
{"type":"FN","symbol":"000001","name":"c","spell":"HXCZHH"}

更新:

我从一个 url 中删除了一些东西,我想让输出可以用函数 json.loads() 处理,那么我该怎么办?

import urllib2
convert_url = "http://quotes.money.163.com/stocksearch/json.do?type=&count=10&word=000001"
req = urllib2.Request(convert_url)
html = urllib2.urlopen(req).read()
html = html.decode("gbk").encode("utf-8")
print html
4

1 回答 1

4

好的,所以 API 返回一个JSONP响应。

因此,首先您必须将格式“解包”或“转换”为普通的 json。我们将使用callbackurl 的参数将 JSONP 函数设置为固定名称

# Setthe callback name to "n"
convert_url = "http://quotes.money.163.com/stocksearch/json.do?type=&count=10&word=000001&callback=n"

然后我们可以去掉 JSONP 留下的 JSON,可以很方便的解析

import json

def loads_jsonp(jsonp, callback_name):
    jsonp = jsonp.strip()
    if jsonp.startswith(callback_name + '(') and jsonp.endswith(')'):
        # remove the leading "callback(" and trailing ")", then parse the json
        return json.loads(jsonp[len(callback_name) + 1:-1])
    raise ValueError.new("Callback names do not match, or invalid JSONP format")

然后我们可以将其应用于我们的数据:

# `jsonp_data` variable contains the body of the response
my_data = loads_jsonp(jsonp_data, "n")

作为替代方案,您可以检查 API 是否能够返回常规 JSON 响应,在这种情况下,我们可以跳过所有这些跳过并直接使用json.loads

于 2013-11-14T07:09:51.683 回答