首页 黄金价格 黄金价格示例代码 黄金价格[Python]
  • 示例代码

本代码示例是基于Python的六派数据接口进行数据请求  API服务请求的代码示例,使用前你需要:

①:通过https://www.6api.net/api/gold/ 申请API服务

以下是完整代码示例:

#!/usr/bin/env python
# -*- coding: utf-8 -*- 
# 测试环境: python2.7
# 安装requests依赖 => pip install requests/ easy_install requests

import requests
import json
import sys 

reload(sys)
sys.setdefaultencoding("utf-8")

def api_send_request(method, url, params=None, headers=None):
   '''
   转发请求到目的主机
   @param method str 请求方法
   @param url str 请求地址
   @param params dict 请求参数
   @param headers dict 请求头
   '''
   method = str.upper(method)
   if method == "POST":
       return requests.post(url=url, data=params, headers=headers)
   elif method == "GET":
       return requests.get(url=url, params=params, headers=headers)
   else:
       return None
 
method = "POST"
url = "http://open.liupai.net/gold/shgold"
headers = None
params = { 
  "appkey" : "yourappsecret",
  "goldid" : "参数1"
}
result = api_send_request(method=method, url=url, params=params, headers=headers)
if result:
   body = result.result
   response = json.loads(body)
   status_code = response["status"]
   if (status_code == "200"): 
       print("请求成功:%s" % (body,))
   else: