【问题标题】:Python: direct requesting with requests.get('http.weatherdataexample.com/example.json')Python:使用 requests.get('http.weatherdataexample.com/example.json') 直接请求
【发布时间】:2020-11-22 10:37:27
【问题描述】:

我正在使用 Python 3.x 和 lib requests。 有没有办法通过从 json 文件中放置整个路径而不首先下载整个 json 来获取特定值?

我有以下代码来获取天气数据:

import requests

data=requests.get('http.weatherdata.com/example.json')
current_wind=a.json()['features'][48]['properties']['value']

有没有办法像下面这样直接请求“当前风”?这可能会减少请求的流量。

import requests

current_wind=requests.get('http.weatherdata.com/example.json',['features'][48]['properties']['value'])
    

我一直在寻找那个特定的问题......几乎放弃了。 谢谢你的回答。

【问题讨论】:

  • 这不是Python问题,而是关于天气数据API的问题。如果可能(我不知道那个 API),您将能够使用 Pyhon 准备足够的请求。

标签: python json api python-requests


【解决方案1】:

请在问题中更准确和/或举例说明。

但我认为你的意思是这样的:

a=requests.get('http.weatherdata.com/example.json')
current_wind=a.json()['features'][48]['properties']['value']

您必须为请求定义变量,然后将其转换为json。

【讨论】:

  • 抱歉我的问题不准确。我的意思是你的代码,但在那里。它首先下载整个 example.json,然后查找特定值。有没有办法直接解析特定值而不先获取整个example.json文件?
  • 我不这么认为 :( 。你为什么不想加载整个文件?是因为 reuqets 模块的速度吗?
  • :(所以我不得不忍受它。更多的是关于无用数据的流量:) 谢谢!
猜你喜欢
  • 2012-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-28
  • 2020-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多