[资料] 如何使用Python实现购物比价API调用的代码实例
594 查看
2 回复
 楼主 | 发布于 2020-01-21 | 只看楼主
分享到:

代码描述:基于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" : "", #排序规则,1、score 按权重从高到底排序,默认  2、price 按价格从底到高排序 3、sell 按销售从高到底排序

        "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()

 


本帖有更多资源,需 登录 才可以下载,没有帐号?立即 注册

(0 ) (0 )
回复 举报

回复于 2020-01-29 沙发

支持下,谢谢分享!
(0 )
评论 (0) 举报

回复于 2020-02-18 2#

感谢分享,欢迎关注我,资料持续更新中。有需要机械臂,电源,硬件电路设计,软件编程,开发板等各种定制的可以私聊我哦,相互学习,共同进步。
(0 )
评论 (0) 举报
  • 发表回复
    0/3000





    举报

    请选择举报类别

    • 广告垃圾
    • 违规内容
    • 恶意灌水
    • 重复发帖

    全部板块

    返回顶部