如何从维基百科JSON响应中获取“提取”键的值

时间:2017-03-30 13:59:09

标签: json python-3.x api

我想在Python3中读取Wikipedia JSON响应中“extract”键的值。我正在测试的网址是https://en.wikipedia.org/w/api.php?action=query&titles=San%20Francisco&prop=extracts&format=json

响应看起来像这样

{% load i18n %}
{% load date %}

{{ customer.civil_title }}{{ user.get_full_name }}                                         
{{ customer.civic_number }}, {{ customer.street }}
{{ customer.rough_location }}

{# customer. date_customerprofile('English') #}


                           7 DAYS NOTICE

Subject:  Overdue account with ...
Client id : {{ customer.id }}

Hello {{ customer.civil_title }}{{ user.get_full_name }},

This notice serves to inform you that your payments with Gestion Multi Finance Inc. are pending. Your file has been transferred to our collection service. The balance of your loan is now:  {{ customer.current_balance }}.

We are asking you to communicate with us without delay, by phone at: 1-855-... ext. 324 or by email at: ... to find a payment solution in order to regulate your situation. If this does not happen, we will be forced to send you a notice of acceleration of the process and transfer your file to our judiciary collection service, with authority to take the necessary steps in the collection process. These steps can include the seizure of salary, but we prefer to avoid these measures.

 Also, it is important to remember that at the time of the loan, you signed a contract in which you agreed to respect the payments. Unfortunately, you have no respected this agreement.

 Ignore this notice if an agreement has already been reached.

Cordially,

{{ email.footer.txt }}

我删除了很多内容。

现在的问题是如何以编程方式读取页码。页码随着不同的搜索而变化。我绝对不想硬编码页码。我该怎么做而不是页码

{
  "batchcomplete": "",
  "query": {
    "pages": {
      "49728": {
        "pageid": 49728,
        "ns": 0,
        "title": "San Francisco",
        "extract": "<p><b>San Francisco</b></p>"
      }
    }
  }
}

有没有办法从pages标签中提取密钥然后继续获取它的值?

2 个答案:

答案 0 :(得分:1)

我发现我可以使用Python的.keys()方法解决这个问题。我做到了。

key = list(response['query']['pages'].keys())
print(response['query']['pages'][key[0]]['extract'])

答案 1 :(得分:1)

使用.keys()方法

执行此操作的一种可能方法

page_number = list(json["query"]["pages"].keys())[0]