《eoLinker-API_Shop_水族宠物大全_API接口_Python调用示例代码》由会员分享,可在线阅读,更多相关《eoLinker-API_Shop_水族宠物大全_API接口_Python调用示例代码(5页珍藏版)》请在金锄头文库上搜索。
1、eoLinker-API Shop 水族水族宠宠物大全物大全 Python调调用示例代用示例代码码水族水族宠宠物大全物大全获取水族宠物的相关信息,包括品种介绍、产地、性格、寿命、常见疾病等信息, 带图片。该产该产品品拥拥有以下有以下APIs: :1.获取水族宠物列表2.关键字获取水族列表3.获取水族动物信息注意,注意,该该示例代示例代码仅码仅适用于适用于 网站下网站下API 使用使用该产该产品前,您需要通品前,您需要通过过 https:/ 申申请请API服服务务1.获获取水族取水族宠宠物列表物列表 #!/usr/bin/env python # -*- coding: utf-8 -*- #
2、 测试环境: python2.7 # 安装requests依赖 = pip install requests/ easy_install requests# 导入requests依赖 import requests import json import sysreload(sys) sys.setdefaultencoding(utf-8)def apishop_send_request(method, url, params=None, headers=None):转发请求到目的主机param method str 请求方法param url str 请求地址param params dic
3、t 请求参数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 Nonemethod = “POST“ url = “https:/ headers = None params = “apiKey“:“your_api_ke
4、y“, #需要从获取 “page“:“ #当前页码 “pageSize“:“ #当前页面水族宠物数量 result = apishop_send_request(method=method, url=url, params=params, headers=headers) if result:body = result.textresponse = json.loads(body)status_code = response“statusCode“if (status_code = 000000):# 状态码为000000, 说明请求成功print(请求成功:%s % (body,)else:
5、# 状态码非000000, 说明请求失败print(请求失败: %s % (body,) else:# 返回内容异常,发送请求失败print(发送请求失败)2.关关键键字字获获取水族列表取水族列表 #!/usr/bin/env python # -*- coding: utf-8 -*- # 测试环境: python2.7 # 安装requests依赖 = pip install requests/ easy_install requests# 导入requests依赖 import requests import json import sysreload(sys) sys.setdefau
6、ltencoding(utf-8)def apishop_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
7、 requests.get(url=url, params=params, headers=headers)else:return Nonemethod = “POST“ url = “https:/ rd“ headers = None params = “apiKey“:“your_api_key“, #需要从获取 “page“:“ #当前页码 “pageSize“:“ #当前页面水族宠物数量 “keyword“:“ #关键字 result = apishop_send_request(method=method, url=url, params=params, headers=heade
8、rs) if result:body = result.textresponse = json.loads(body)status_code = response“statusCode“if (status_code = 000000):# 状态码为000000, 说明请求成功print(请求成功:%s % (body,)else:# 状态码非000000, 说明请求失败print(请求失败: %s % (body,) else:# 返回内容异常,发送请求失败print(发送请求失败)3.获获取水族取水族动动物信息物信息 #!/usr/bin/env python # -*- coding:
9、utf-8 -*- # 测试环境: python2.7 # 安装requests依赖 = pip install requests/ easy_install requests# 导入requests依赖 import requests import json import sysreload(sys) sys.setdefaultencoding(utf-8)def apishop_send_request(method, url, params=None, headers=None):转发请求到目的主机param method str 请求方法param url str 请求地址param
10、 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 Nonemethod = “POST“ url = “https:/ headers = None params = “apiKey“:“
11、your_api_key“, #需要从获取 “petID“:“ #宠物ID result = apishop_send_request(method=method, url=url, params=params, headers=headers) if result:body = result.textresponse = json.loads(body)status_code = response“statusCode“if (status_code = 000000):# 状态码为000000, 说明请求成功print(请求成功:%s % (body,)else:# 状态码非000000, 说明请求失败print(请求失败: %s % (body,) else:# 返回内容异常,发送请求失败print(发送请求失败)