1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| #getSgeQutn.py 获得上金所每日详细价格数据 #Copyright © Ande
#v1 20180416 #v2 20181203 加入日志记录log #v3 增加熊猫金币合约PGC30g #v4 20190102 原数据地址由http升级为https. 若使用url = 'http://www.sge.com.cn/graph/quotations'只能请求到Au99.99, 改为url = 'https://www.sge.com.cn/graph/quotations'方能正常 #v5 20191016 增加两个纽约金延期产品,合约代码为NYAuTN06,NYAuTN12 #v6 20191029 修正ssl.SSLCertVerificationError requests.exceptions.SSLError #v7 20200104 1.读取json自身数据命名 2.判别月份目录
import os import requests import datetime import time import json
# 禁用安全请求警告 import urllib3 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
#writeToHead() 在文件开头写入内容 def writeToHead(new, file = './' + datetime.datetime.now().strftime("%Y%m") + '/log.txt'): try: with open(file,'r+') as f: old = f.read() f.seek(0,0) f.write(new +'\n' + old) except FileNotFoundError: with open(file,'a+') as f: f.write(new) except PersmissionError: pass url = 'https://www.sge.com.cn/graph/quotations'#数据url地址
cookies = { 'Hm_lvt_9165e30c92968eb1baa5e3eb5f34dc60': '1546584532', 'JSESSIONID': '2A113D952975290E4A1A4A9353B4AAC3', 'Hm_lpvt_9165e30c92968eb1baa5e3eb5f34dc60': '1546584727', }
headers = { 'Host': 'www.sge.com.cn', 'User-Agent': 'Mozilla/5.0 (Windows NT 5.1; rv:52.0) Gecko/20100101 Firefox/52.0', 'Accept': 'application/json, text/javascript, */*; q=0.01', 'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3', 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'X-Requested-With': 'XMLHttpRequest', 'Referer': 'https://www.sge.com.cn/', 'Connection': 'keep-alive', }
heyue = [ "Au99.99", "Au99.95", "Au100g", "Pt99.95", "Ag(T+D)", "Au(T+D)", "mAu(T+D)", "Au(T+N1)", "Au(T+N2)", "Ag99.99", "Ag99.9", "iAu99.99", "Au99.5", "iAu100g", "iAu99.5", "Au50g", "PGC30g", "NYAuTN06", "NYAuTN12", ]
dt_ = datetime.datetime.now() writeToHead('\n')#日志头标志 ---YYYYMMDDHHMMSS---
for hy in heyue:
data = {'instid': hy} response = requests.post(url, headers=headers, cookies=cookies, data=data, verify=False) json_str = response.text.encode('utf-8') json_dict = json.loads(json_str)
hy = json_dict["heyue"]#获取json中合约 dt = json_dict["delaystr"]#获取json中最后更新时间 dt = time.strptime(dt, '%Y年%m月%d日 %H:%M:%S')#将获取的更新时间转成时间元组 dir_dt = time.strftime('%Y%m',dt)#将时间元组格式化成YYYYmm用作文件时间目录 file_dt = time.strftime('%Y%m%d%H%M%S',dt)#将时间元组格式化成YYYYmmddHHMMSS用作文件名时间部分 if not os.path.exists(dir_dt): os.makedirs(dir_dt)
file = './' + dir_dt + '/' + hy +'_' + file_dt + '.json' with open(file, "w+") as f: json.dump(json_dict,f)
writeToHead(file_dt + '\t' + hy)#日志记录 progressBar = str(heyue.index(hy)+1) + '/' + str(len(heyue))#进度条 print(progressBar + '\t' + file_dt + '\t' + hy)
writeToHead('\n---' + dt_.strftime('%Y%m%d%H%M%S') + '---')#日志头标志 ---YYYYMMDDHHMMSS---
|