【问题标题】:Struggling to understand the Guardian API via Python努力通过 Python 理解 Guardian API
【发布时间】:2023-04-03 14:00:01
【问题描述】:

我在 Windows Vista 64 位上使用 Python.org 版本 2.7 64 位。我整理了一些代码,这些代码结合了使用 Guardian 支持团队提供的 API 密钥的身份验证方法和他们网站的 Content API 代码生成器生成的一些 Javascript:

import requests 
def get_content():
    api_url = 'http://content.guardianapis.com/#/search?q=football'
    payload = {
        'api-key':              '',
        'page-size':            10,
        'show-editors-picks':   'true',
        'show-elements':        'image',
        'show-fields':          'all'

    }
    response = requests.get(api_url, params=payload)
    data = response.json() # convert json to python-readable format
    return data
    print data


{
  "response": {
    "status": "ok",
    "userTier": "free",
    "total": 1,
    "results": [
      {
        "id": "football",
        "webTitle": "Football",
        "webUrl": "http://www.theguardian.com/football",
        "apiUrl": "http://content.guardianapis.com/football",
        "editions": [
          {
            "id": "football",
            "webTitle": "Football",
            "webUrl": "http://www.theguardian.com/football",
            "apiUrl": "http://content.guardianapis.com/football",
            "code": "default"
          }
        ]
      }
    ]
  }
}

我对 Python 还很陌生,对 Javascript 了解不多。我认为这段代码会将结果从他们网站的足球部分打印到 Python IDLE。它运行没有错误,但它没有产生任何输出。

谁能告诉我我做错了什么和/或我是否完全误解了这段代码的目的?

谢谢

【问题讨论】:

  • 我认为“返回数据”需要在“打印数据”之后进行
  • @imcg 您好,感谢您的回复。我已经尝试过两种方式。不幸的是,两者都不起作用......
  • 你如何在空闲时运行/调用它?
  • @PadraicCunningham F5 - 运行模块
  • @user3045351,我的答案中的代码应该使用 f5 run 模块运行。

标签: python json api


【解决方案1】:

你必须调用函数来获得输出:

get_content() # you need to call the function


import requests
def get_content():
    api_url = 'http://content.guardianapis.com/#/search?q=football'
    payload = {
        'api-key':              'xxxxxxxxxxx',
        'page-size':            10,
        'show-editors-picks':   'true',
        'show-elements':        'image',
        'show-fields':          'all'

    }
    response = requests.get(api_url, params=payload)
    data = response.json() # convert json to python-readable format
    print  data  # put print first
    return data
get_content() # call function

同时拥有return data before print data 意味着print data 无法访问,因为您的函数在您return data 时结束。

【讨论】:

  • 我使用了那个确切的语法,并得到一个关于 api 密钥无效的错误!我不知道为什么我昨天在谷歌群组上给监护人发了消息,他们对我说他们已经确认密钥是有效的(请你修改你的答案并为我删除它吗?我把它包含在错误中 - 谢谢) ...我从我的网络浏览器复制并粘贴了 API 密钥...你认为我需要手动复制它吗?
  • 其实我以为你已经把钥匙弄好了,因为有一个错误,我想复制应该没问题,但值得一试。附带说明一下,我建议使用ipython,您可以复制代码,然后在 ipython 中输入paste,然后使用my_func() 运行。 ipython 中有很多非常酷的功能
  • ok 会看看那个。至于 api 密钥,我在 google 群组上再次向他们发送了消息,并将在一分钟内向他们发送另一条推文。在我最初的问题中,在 print 和 return 语句下方,所有这些代码的目的是什么?如果您是初学者,内容代码生成器并不容易理解。谢谢
  • 你的意思是你问题底部的json吗?
  • 是的,那一点。他们提供了这个内容代码生成器:'explorer.content.guardianapis.com/#/… 显然可以在您添加过滤器元素时看到代码更改,但我不确定在哪个上下文中使用此代码......就像它实际上在做的那样?
猜你喜欢
  • 1970-01-01
  • 2015-09-29
  • 1970-01-01
  • 2021-02-23
  • 2011-12-30
  • 1970-01-01
  • 1970-01-01
  • 2015-01-20
  • 1970-01-01
相关资源
最近更新 更多