如何编写正则表达式来搜索这样的字符串?

时间:2013-11-14 06:44:22

标签: python regex

_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

1 个答案:

答案 0 :(得分:4)

好的,API正在返回JSONP响应。

所以,首先你必须将该格式“解包”或“转换”为普通的json。我们将使用url的callback参数将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