如何使用发布请求获取json数据

时间:2020-03-04 05:29:04

标签: json python-3.x pandas python-requests

我正在尝试获取json数据并将其转换为Dataframe 贝娄是我的代码

import sys, os, time, time
import pandas as pd
import json
import requests
from pandas.io.json import json_normalize

url = "https://www.theglobaleconomy.com/ajax/ajax_get_one_country_graph_monthly.php" 

DataAll = pd.DataFrame()
headers = {
           'Accept-Encoding': 'gzip, deflate, br',
           'Accept-Language': 'en-US,en;q=0.9',
           'Connection': 'keep-alive',
           'Content-Length': '86',
           'Content-Type': 'application/x-www-form-urlencoded',
           'Cookie': 'cookiesNotification=bgv%40%21%23; PHPSESSID=3430d15c8295340d340984f4726c171d; __gads=ID=cbac903191e62d31:T=1583214794:S=ALNI_MbEMX0Tr0sqGlTq_GDjYVQ9Qitq0Q; howToFind=1; __utma=164978938.789649024.1582004744.1583247321.1583296676.5; __utmc=164978938; __utmz=164978938.1583296676.5.5.utmcsr=knoema.com|utmccn=(referral)|utmcmd=referral|utmcct=/WLDTGEPMI2019/purchasing-managers-index-pmi; __utmt=1; __utmb=164978938.2.10.1583296676',
           'Host': 'www.theglobaleconomy.com',
           'Origin': 'https://www.theglobaleconomy.com',
           'Referer': 'https://www.theglobaleconomy.com/Australia/pmi_composite/',
           'Sec-Fetch-Dest': 'empty',
           'Sec-Fetch-Mode': 'cors',
           'Sec-Fetch-Site': 'same-origin',
           'User-Agent': 'Mozilla/5.0',
          }
#Form data taken as params
params =  {
           'yearFrom': '2019',
           'monthFrom': '1',
           'yearTo': '2020',
           'monthTo': '12',
           'countryId': '160',
           'indicator': 'pmi_composite'
          }

try:
    Res = requests.post(url, headers = headers,data= params, allow_redirects=False)
    print(Res.text)
except requests.exceptions.ConnectionError:
    Res.status_code = "Connection refused"

if not Res == None:
   JsonData = json.loads(Res.text)
   Data = json_normalize(JsonData['data'])
   DataAll = DataAll.append(Data)

我收到以下错误提示 从None提高JSONDecodeError(“期望值”,s,err.value) json.decoder.JSONDecodeError:期望值:第1行第1列(字符0)

这是json预览的样子

true | [{{“ year”:“ 2019”,“ month”:“ 1”,“ value”:51.3,“ country_id”:“ 160”},{“ year”:“ 2019”,“ month” :“ 2”,“值”:52.9,“ country_id”:“ 160”},{“年”:“ 2019”,“月”:“ 3”,“值”:49.5,“ country_id”:“ 160” },{“ year”:“ 2019”,“ month”:“ 4”,“ value”:50,“ country_id”:“ 160”},{“ year”:“ 2019”,“ month”:“ 5” ,“ value”:51.5,“ country_id”:“ 160”},{“ year”:“ 2019”,“ month”:“ 6”,“ value”:52.5,“ country_id”:“ 160”},{“ year“:” 2019“,” month“:” 7“,” value“:52.1,” country_id“:” 160“},{” year“:” 2019“,” month“:” 8“,” value“ :49.3,“ country_id”:“ 160”},{“ year”:“ 2019”,“ month”:“ 9”,“ value”:52,“ country_id”:“ 160”},{“ year”:“ 2019“,” month“:” 10“,” value“:50,” country_id“:” 160“},{” year“:” 2019“,” month“:” 11“,” value“:49.7,” country_id“:” 160“},{” year“:” 2019“,” month“:” 12“,” value“:49.8,” country_id“:” 160“},{” year“:” 2020“,” month“:” 1“,” value“:50.2,” country_id“:” 160“},{” year“:” 2020“,” month“:” 2“,” value“:48.3,” country_id“:” 160“}] |索引点

请帮助我输入代码。

0 个答案:

没有答案