正则表达式 :
s = re.findall("\((.*)\)",str(html))[0]
from urllib.request import *
import re
import json
fp = urlopen('http://webforex.hermes.hexun.com/forex/quotelist?code=FOREXUSDCNY,&column=code,price,UpdownRate&callback=ongetjsonpforex&_=1451543515359')
html = fp.read().decode("utf-8")
fp.close()
s = re.findall("\((.*)\)",str(html))[0]
sjson = json.loads(s)
USDCNY = sjson["Data"][0][0][1]/10000
print(USDCNY)
定义函数:
import urllib.request
def getUSDCNY(self): fp = urllib.request.urlopen( 'http://webforex.hermes.hexun.com/forex/quotelist?code=FOREXUSDCNY,&column=code,price,UpdownRate&callback=ongetjsonpforex&_=1451543515359') html = fp.read().decode("utf-8") fp.close() s = re.findall("\((.*)\)", str(html))[0] sjson = json.loads(s) USDCNY = sjson["Data"][0][0][1] / 10000 return USDCNY