【问题标题】:Using Python for JSON calls [closed]使用 Python 进行 JSON 调用 [关闭]
【发布时间】:2013-03-09 22:32:24
【问题描述】:

这是我尝试使用的类

class Products(Resource):
    """The collection of products in a store"""

    def get(self):
        """Returns list of products"""
        products_list = self.client.request_json('GET', '/products?limit=%s&page=%s' % (self.paginate_by, self.current_page))
        return [Product(product) for product in products_list]

这是我的脚本:

import bigcommerce.api

bigcommerce.api.Connection.host = 'https://store-qvek.mybigcommerce.com'
bigcommerce.api.Connection.user = 'admin'
bigcommerce.api.Connection.api_key = '272956b18e3a7c269b413385908cc7371f5c41'


products_list = bigcommerce.api.Products.get()
for product in products_list:
    print product.name

我在 Products.get() 中遗漏了一些东西....但是什么?

Traceback (most recent call last):
  File "C:\wget\bin\test_bigcommerce.py", line 8, in <module>
    products_list = bigcommerce.api.Products.get()
TypeError: unbound method get() must be called with Products instance as first argument (got nothing instead) 

P.S 由于我更改了主机和 api 密钥以便将其公开发布,因此代码将无法正常工作。

【问题讨论】:

  • 实际问题是什么?预期和实际结果是什么?
  • 我正在尝试使用 Python 获取我的产品列表......当我运行我的代码时,我收到以下错误......回溯(最近一次调用):文件“C :\wget\bin\test_bigcommerce.py", line 8, in products_list = bigcommerce.api.Products.get() TypeError: unbound method get() must be called with Products instance 作为第一个参数(什么都没有)
  • 问题显然出在bigcommerce 模块或者你如何使用它。您应该查看文档和/或在更具体的论坛中提问。没有更多信息,您不可能得到答案。
  • 不幸的是,bigcommerce python api 的文档很少……我对 python 还是很陌生,对使用 JSON 调用网站真的很陌生……这是怎么做到的“('GET', '/products?limit=%s&page=%s' % (self.paginate_by, self.current_page))" 翻译成 .get() 调用?
  • @user2152584:string % tuple 是 Python 格式运算符。结果是另一个字符串,其中 '%s' 被替换为元组的值。该字符串可能直接用作要获取的 url。

标签: python json


【解决方案1】:

您有一个类 Products,但从不创建它的实例。

改为创建一个实例:

bigcommerce.api.Products().get()

【讨论】:

    猜你喜欢
    • 2019-12-24
    • 1970-01-01
    • 2021-01-11
    • 1970-01-01
    • 2020-09-20
    • 2011-02-15
    • 1970-01-01
    • 2021-01-16
    • 2011-01-06
    相关资源
    最近更新 更多