基于Python的购物比价api调用代码实例

2022-05-19 13:39:23   文档大全网     [ 字体: ] [ 阅读: ]

#文档大全网# 导语】以下是®文档大全网的小编为您整理的《基于Python的购物比价api调用代码实例》,欢迎阅读!
比价,调用,实例,基于,代码
基于Python的购物比价api调用代码实例



代码描述:基于Python的购物比价api调用代码实例 代码平台:聚合数据



#!/usr/bin/python

# -*- coding: utf-8 -*- import json, urllib

from urllib import urlencode



#----------------------------------

# 商品比价调用示例代码 聚合数据

# 在线接口文档:http://www.juhe.cn/docs/137 #----------------------------------



def main():



#配置您申请的APPKey

appkey = "*********************"



#1.查询支持的商城信息 request1(appkey,"GET")



#2.比价简单查询接口

request2(appkey,"GET")



#3.比价复杂查询接口

request3(appkey,"GET")



#查询支持的商城信息

def request1(appkey, m="GET"):

url = "http://api2.juheapi.com/mmb/allsites" params = {

"key" : appkey, #应用APPKEY(应用详细页查询)



}

params = urlencode(params) if m =="GET":

f = urllib.urlopen("%s?%s" % (url, params))


else:

f = urllib.urlopen(url, params)



content = f.read()

res = json.loads(content) if res:

error_code = res["error_code"] if error_code == 0:

#成功请求

print res["result"] else:

print "%s:%s" % (res["error_code"],res["reason"]) else:

print "request api error"



#比价简单查询接口

def request2(appkey, m="GET"):

url = "http://api2.juheapi.com/mmb/search/simple" params = {

"keyword" : "", #要查询关键字

"key" : appkey, #应用APPKEY(应用详细页查询)



}

params = urlencode(params) if m =="GET":

f = urllib.urlopen("%s?%s" % (url, params)) else:

f = urllib.urlopen(url, params)



content = f.read()

res = json.loads(content) if res:

error_code = res["error_code"] if error_code == 0:

#成功请求

print res["result"] else:

print "%s:%s" % (res["error_code"],res["reason"]) else:

print "request api error"



#比价复杂查询接口

def request3(appkey, m="GET"):

url = "http://api2.juheapi.com/mmb/search/complex"


params = {

"keyword" : "", #搜索关键字

"key" : appkey, #应用APPKEY(应用详细页查询) "Site" : "", #商城编号,默认为0,即所有

"PriceMin" : "", # 最低价格,0表示无最低价格限制 "PriceMax" : "", #最高价格,0表示无最高价格限制 "PageNum" : "", #页号,大于0

"PageSize" : "", #每页返回结果数,上限为50

"Orderby" : "", #排序规则,1score 按权重从高到底排序,默认 2price 按价格从底到高排序 3sell 销售从高到底排序 "ZiYing" : "", #是否自营

"ExtraParameter" : "", #0:结果包含淘宝数据,1:结果不包含淘宝数据



}

params = urlencode(params) if m =="GET":

f = urllib.urlopen("%s?%s" % (url, params)) else:

f = urllib.urlopen(url, params)



content = f.read()

res = json.loads(content) if res:

error_code = res["error_code"] if error_code == 0:

#成功请求

print res["result"] else:

print "%s:%s" % (res["error_code"],res["reason"]) else:

print "request api error"



if __name__ == '__main__': main()


本文来源:https://www.wddqxz.cn/8e5e936865ce050877321303.html

相关推荐