通常情况下我们利用python的正则表达式re.findall等得到的结果为json数组,如果我们需要对这个json串进行其他的分析,保存成文件是一个不错的选择,如下python代码为演示。
#coding=utf-8
import re
import json
pattern = r'http://quote.eastmoney.com/{1}([a-z]{2}[0-9]{6}).html'
regex = re.compile(pattern)
ip = open('a.html').read()
rls = regex.findall(ip)
ds = json.dumps(rls)
f1 = open('code.txt', 'wb')
f1.write(ds)
f1.close()
print "JSON:", ds
程序中,我们将a.html中匹配到的内容,输出到code.txt中。