雾岚 发表于 2022-6-29 18:56:55

requests发送请求转json出现报错

发送get请求使用json解码出现报错r = requests.get('https://data.weather.gov.hk/weatherAPI/opendata/weather.php?', data = {'dataTpye':'fnd', 'lang':'sc'})
r_dict = r.json()
print(r_dict)
错误提示是raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 2)
我使用浏览器直接访问https://data.weather.gov.hk/weatherAPI/opendata/weather.php?dataType=fnd&lang=sc,可以得到json格式返回数据{"generalSituation":"位于南海中部的热带气旋会在未来两三日大致移向广东西部沿岸一带,并逐渐增强,南海北部及华南沿岸风势逐渐增强,海有涌浪。其相关的外围雨带亦会为该区带来狂风骤雨及雷暴。受活跃偏南气流影响,预料下周广东沿岸天气持续不稳定。","weatherForecast":[{"forecastDate":"20220630","week":"星期四","forecastWind":"东风4至5级,稍后离岸及高地6级。","forecastWeather":"大致多云,有几阵骤雨,骤雨渐转频密及有狂风雷暴。海有涌浪。","forecastMaxtemp":{"value":31,"unit":"C"},"forecastMintemp":{"value":27,"unit":"C"},"forecastMaxrh":{"value":90,"unit":"percent"},"forecastMinrh":{"value":70,"unit":"percent"},"ForecastIcon":62,"PSR":"中"},{"forecastDate":"20220701","week":"星期五","forecastWind":"东风5至6级,高地间中7级。","forecastWeather":"多云,有狂风骤雨及雷暴。海有涌浪。","forecastMaxtemp":{"value":30,"unit":"C"},"forecastMintemp":{"value":26,"unit":"C"},"forecastMaxrh":{"value":95,"unit":"percent"},"forecastMinrh":{"value":75,"unit":"percent"},"ForecastIcon":63,"PSR":"高"},{"forecastDate":"20220702","week":"星期六","forecastWind":"东至东南风5级,间中6级。","forecastWeather":"多云,有狂风骤雨及雷暴,雨势有时颇大。海有涌浪。","forecastMaxtemp":{"value":29,"unit":"C"},"forecastMintemp":{"value":26,"unit":"C"},"forecastMaxrh":{"value":95,"unit":"percent"},"forecastMinrh":{"value":80,"unit":"percent"},"ForecastIcon":64,"PSR":"高"},{"forecastDate":"20220703","week":"星期日","forecastWind":"南至东南风5级。","forecastWeather":"大致多云,间中有狂风骤雨及雷暴,初时雨势有时颇大。","forecastMaxtemp":{"value":29,"unit":"C"},"forecastMintemp":{"value":26,"unit":"C"},"forecastMaxrh":{"value":95,"unit":"percent"},"forecastMinrh":{"value":80,"unit":"percent"},"ForecastIcon":63,"PSR":"高"},{"forecastDate":"20220704","week":"星期一","forecastWind":"南风4至5级。","forecastWeather":"大致多云,间中有骤雨及雷暴。","forecastMaxtemp":{"value":30,"unit":"C"},"forecastMintemp":{"value":27,"unit":"C"},"forecastMaxrh":{"value":95,"unit":"percent"},"forecastMinrh":{"value":80,"unit":"percent"},"ForecastIcon":63,"PSR":"高"},{"forecastDate":"20220705","week":"星期二","forecastWind":"南风4至5级。","forecastWeather":"大致多云,间中有骤雨及雷暴。","forecastMaxtemp":{"value":30,"unit":"C"},"forecastMintemp":{"value":27,"unit":"C"},"forecastMaxrh":{"value":95,"unit":"percent"},"forecastMinrh":{"value":80,"unit":"percent"},"ForecastIcon":63,"PSR":"高"},{"forecastDate":"20220706","week":"星期三","forecastWind":"南风4级,间中5级。","forecastWeather":"大致多云,间中有骤雨及雷暴。","forecastMaxtemp":{"value":30,"unit":"C"},"forecastMintemp":{"value":27,"unit":"C"},"forecastMaxrh":{"value":95,"unit":"percent"},"forecastMinrh":{"value":80,"unit":"percent"},"ForecastIcon":63,"PSR":"中高"},{"forecastDate":"20220707","week":"星期四","forecastWind":"南风4级,间中5级。","forecastWeather":"大致多云,间中有骤雨及雷暴。","forecastMaxtemp":{"value":30,"unit":"C"},"forecastMintemp":{"value":27,"unit":"C"},"forecastMaxrh":{"value":95,"unit":"percent"},"forecastMinrh":{"value":80,"unit":"percent"},"ForecastIcon":63,"PSR":"中高"},{"forecastDate":"20220708","week":"星期五","forecastWind":"南风4级。","forecastWeather":"大致多云,有几阵骤雨。","forecastMaxtemp":{"value":31,"unit":"C"},"forecastMintemp":{"value":27,"unit":"C"},"forecastMaxrh":{"value":95,"unit":"percent"},"forecastMinrh":{"value":75,"unit":"percent"},"ForecastIcon":62,"PSR":"中"}],"updateTime":"2022-06-29T16:40:00+08:00","seaTemp":{"place":"北角","value":28,"unit":"C","recordTime":"2022-06-29T14:00:00+08:00"},"soilTemp":[{"place":"香港天文台","value":30.2,"unit":"C","recordTime":"2022-06-29T07:00:00+08:00","depth":{"unit":"metre","value":0.5}},{"place":"香港天文台","value":29.1,"unit":"C","recordTime":"2022-06-29T07:00:00+08:00","depth":{"unit":"metre","value":1}}]}
我看了json的返回结果,所有的数字值都没有加双引号。请问为什么requests转换json时会出现报错,是因为返回值数字没有双引号的原因吗

suchocolate 发表于 2022-6-30 13:58:11

本帖最后由 suchocolate 于 2022-6-30 14:05 编辑

import requests

url = 'https://data.weather.gov.hk/weatherAPI/opendata/weather.php'
params = {'dataType': 'fnd', 'lang': 'sc'}
r = requests.get(url, params=params)
print(r.json())
dataType写成了Tpye,参数用params传输,表单数据用data,不是同一个东西。
另外你发现json有问题,可以print一下r.text,你会发现官网给你提示了,让你去对照api手册重新调整参数。
页: [1]
查看完整版本: requests发送请求转json出现报错