通过请求获取特定的JSON元素 [英] Get specific JSON elements with requests

查看:86
本文介绍了通过请求获取特定的JSON元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过Pythonrequests发出JSON请求,在这里我只获得某些JSON对象.

I want to make a JSON request with the Python library requests where I only obtain certain JSON objects.

我知道处理获得的JSON对象只关注所需的信息确实很容易,但是这会限制请求效率(如果重复执行的话).

I know that it is really easy to process the JSON object obtained to only focus in the needed information, but that would throttle the request efficiency (in case it is done repeatedly).

如前所述,我知道这是有可能的:

As said, I know this is a possibility:

url = 'www.foo.com'
r = requests.get(url).json()
#Do something with r[3]['data4'], the only one who is going to be used.

但是我怎么能直接从请求中直接获取r[3]['data4']?

But how could I directly only obtain r[3]['data4'] from the request?

推荐答案

简短回答

要回答您的问题,您不能不理解为什么您需要知道幕后发生的事情.

Short Answer

To answer your question no, you can't but to understand why you need to know what is happening behind the scenes.

在发出诸如r = requests.get('www.foo.bar')之类的请求时,您正在向服务器发出请求,并且在执行r.json()时正在查看该请求的结果.这意味着您不能在解析服务器发送给您的内容时获取r[3]['data'],除非服务器仅发送r[3]['data'].可能可以过滤掉响应处理之外的所有其他内容,但是我不知道该怎么做.

When you make a request such as r = requests.get('www.foo.bar') you are making a request to the server and you are viewing the result of that request when you do r.json(). This means that you cannot just get r[3]['data'] as you are parsing what the server sends to you unless the server only sends r[3]['data']. It may be possible to filter out everything else apart from that in the response processing but I am unaware of how to do it.

这篇关于通过请求获取特定的JSON元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆