首页 > 解决方案 > 处理 request.content 时出现 json.loads 错误

问题描述

运行此代码我收到错误:额外数据:第 1 行第 12 列(字符 11)

url = "https://www.airnowapi.org/aq/forecast/zipCode/?format=text/csv&zipCode=19020&date=2020-10-05&distance=25&API_KEY=......"

try:
    api_request = requests.get(url)
    api = json.loads(api_request.content)
    
except Exception as e:
    print(e)

这是print(api_request.content)的输出我在第 12 列中没有看到任何奇怪的东西。我在这里遗漏了什么?

周六大部分时间阳光明媚,气温仅恢复到 60 度左右。足够的干燥空气将保持在颗粒保持在良好范围内的位置。*** 周日\xe2\x80\x99s 预报:周日大部分时间阳光明媚,但到傍晚时可能会出现几朵云。由于混合较少且风较轻,该地区部分地区有可能出现中低颗粒物水平。*** 周一\xe2\x80\x99s 预报:周日晚上到周一早上会有阵雨,然后可能在当天晚些时候有一点阳光。如果下午有足够的混合,细颗粒物浓度可能会回落到良好范围的上限。*** 扩展预报:周二至周三,西南风将带来较温和的空气。下一个冷锋将在周三晚上移动,并在本周晚些时候带来另一个冷却时间。周二空气质量适中,否则 PM2.5 水平将在本周余下的大部分时间处于较低水平。---McAuliffe "\n"2020-10-02 ","2020-10-06 ","Philadelphia ","PA","39.95","-75.151","PM2.5","53","2","中等","false","当前情况:本周五太阳破云下午。在凉爽干净的气团到位的情况下,细颗粒物在良好范围内。随着远离城市地区的气温回落到 40 多度左右,今晚的天气将主要是月光和寒冷。*** 周六\xe2\x80\x99s 预报:周六大部分时间阳光明媚,气温仅恢复到 60 度中上端。足够的干燥空气将保持在颗粒保持在良好范围内的位置。*** 周日\xe2\x80\x99s 预报:周日大部分时间阳光明媚,但到傍晚时可能会出现几朵云。由于混合较少且风较轻,该地区部分地区有可能出现中低颗粒物水平。*** 周一\xe2\x80\x99s 预报:周日晚上到周一早上会有阵雨,然后可能在当天晚些时候有一点阳光。如果下午有足够的混合,细颗粒物浓度可能会回落到良好范围的上限。*** 扩展预报:周二至周三,西南风将带来较温和的空气。下一个冷锋将在周三晚上移动,并在本周晚些时候带来另一个冷却时间。周二空气质量适中,

标签: pythonjsonpython-requests

解决方案


你的网址有参数format=text/csv。你要format=json

import requests
import json
url = "https://www.airnowapi.org/aq/forecast/zipCode/?format=json&zipCode=19020&date=2020-10-05&distance=25&API_KEY=YOUR_API_KEY"


api_request = requests.get(url)
json_data = api_request.json() # beter use the convenient method of Response object
print(json.dumps(json_data, indent=4)) # this is just to show the response

输出

[
    {
        "DateIssue": "2020-10-02 ",
        "DateForecast": "2020-10-05 ",
        "ReportingArea": "Philadelphia",
        "StateCode": "PA",
        "Latitude": 39.95,
        "Longitude": -75.151,
        "ParameterName": "PM2.5",
        "AQI": 48,
        "Category": {
            "Number": 1,
            "Name": "Good"
        },
        "ActionDay": false,
        "Discussion": "Current conditions: The sun has broken through the clouds this Friday afternoon. With a cool and clean air mass in place, fine particulate is down inside the good range. Tonight will turn out mostly moonlit and chilly as temperatures fall back to the middle 40s away from urban areas.  ***  Saturday\u2019s forecast: Sunshine for the most part Saturday with temperatures only recovering into the middle and upper 60s. Enough dry air will remain present to where particles remain in the good range.  ***  Sunday\u2019s forecast: Sunday will continue with sunshine most of the day, but a few clouds may show up towards evening. With less mixing and lighter winds, there is the chance for low moderate particle levels in parts of the area.  ***   Monday\u2019s forecast: Showers will occur Sunday night into Monday morning, then perhaps a little sun is possible late in the day. If there is enough afternoon mixing, fine particulate concentrations may retreat to the upper good range.  ***  Extended forecast: Milder air will be transported in on southwesterly winds Tuesday into Wednesday. The next cold front will then move through Wednesday night, and bring another cooldown behind it later on in the week. Moderate air quality is again a possibility Tuesday, otherwise PM2.5 levels will be low most of the remainder of the week.---McAuliffe "
    },
    {
        "DateIssue": "2020-10-02 ",
        "DateForecast": "2020-10-06 ",
        "ReportingArea": "Philadelphia",
        "StateCode": "PA",
        "Latitude": 39.95,
        "Longitude": -75.151,
        "ParameterName": "PM2.5",
        "AQI": 53,
        "Category": {
            "Number": 2,
            "Name": "Moderate"
        },
        "ActionDay": false,
        "Discussion": "Current conditions: The sun has broken through the clouds this Friday afternoon. With a cool and clean air mass in place, fine particulate is down inside the good range. Tonight will turn out mostly moonlit and chilly as temperatures fall back to the middle 40s away from urban areas.  ***  Saturday\u2019s forecast: Sunshine for the most part Saturday with temperatures only recovering into the middle and upper 60s. Enough dry air will remain present to where particles remain in the good range.  ***  Sunday\u2019s forecast: Sunday will continue with sunshine most of the day, but a few clouds may show up towards evening. With less mixing and lighter winds, there is the chance for low moderate particle levels in parts of the area.  ***   Monday\u2019s forecast: Showers will occur Sunday night into Monday morning, then perhaps a little sun is possible late in the day. If there is enough afternoon mixing, fine particulate concentrations may retreat to the upper good range.  ***  Extended forecast: Milder air will be transported in on southwesterly winds Tuesday into Wednesday. The next cold front will then move through Wednesday night, and bring another cooldown behind it later on in the week. Moderate air quality is again a possibility Tuesday, otherwise PM2.5 levels will be low most of the remainder of the week.---McAuliffe "
    }
]

我替换了你的 API KEY。你不应该发布它。

编辑:这是重构版本,在我看来更好/更清晰

import requests
import json

API_KEY = 'YOUR-API-KEY'
payload = {'format':'json',
           'zipCode':'19020',
           'date':'2020-10-05',
           'distance':'25',
           'API_KEY':API_KEY}

url = "https://www.airnowapi.org/aq/forecast/zipCode/"

response = requests.get(url, params=payload)
json_data = response.json()
print(json.dumps(json_data, indent=4)) # this is just to show the response

推荐阅读