繁体   English   中英

来自谷歌开发者工具的 JSON 链接在 Python(或浏览器)中不起作用

[英]JSON link from google developer tools not working in Python (or in browser)

我正在尝试提取表中的数据https://www.ecoregistry.io/emit-certifications/ra/10

使用 google developer tools.network 选项卡,我可以获得存储此表数据的 json 链接: https://api-front.ecoregistry.io/api/project/10/emitcertifications

我可以手动复制这个 json 数据并使用我编写的这段代码提取信息:

import json
import pandas as pd
data = '''PASTE JSON DATA HERE'''
info = json.loads(data)
columns = ['# Certificate', 'Carbon offsets destination', 'Final user', 'Taxpayer subject','Date','Tons delivered']
dat = list()
for x in info['emitcertifications']:
dat.append([x['consecutive'],x['reasonUsingCarbonOffsets'],x['userEnd'],x['passiveSubject'],x['date'],x['quantity']])
df = pd.DataFrame(dat,columns=columns)
df.to_csv('Data.csv')

我想自动化它,以便我可以从 json 链接中提取数据: https://api-front.ecoregistry.io/api/project/10/emitcertifications直接而不是手动粘贴 json 数据:

data = '''PASTE JSON DATA HERE'''

该链接在 python 中甚至直接在浏览器中都不起作用:

import requests
import json
url = ('https://api-front.ecoregistry.io/api/project/10/emitcertifications')
response = requests.get(url)
print(json.dumps(info, indent=4))

我得到的错误 output 是:{'status': 0, 'codeMessages': [{'codeMessage': 'ERROR_401', 'param': 'invalid', 'message': 'No autorizado'}]}

当我从开发人员工具下载数据时,这本字典有 'status':1 之后所有数据都在那里。

编辑:我尝试将请求标头添加到 url 但它仍然不起作用:

import requests
import json
url = ('https://api-front.ecoregistry.io/api/project/10/emitcertifications')
hdrs = {"accept": "application/json","accept-language": "en-IN,en;q=0.9,hi-IN;q=0.8,hi;q=0.7,en-GB;q=0.6,en-US;q=0.5","authorization": "Bearer null", "content-type": "application/json","if-none-match": "W/\"1326f-t9xxnBEIbEANJdito3ai64aPjqA\"", "lng": "en", "platform": "ecoregistry","sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"100\", \"Google Chrome\";v=\"100\"", "sec-ch-ua-mobile": "?0", "sec-ch-ua-platform": "\"Windows\"", "sec-fetch-dest": "empty","sec-fetch-mode": "cors", "sec-fetch-site": "same-site" }
response = requests.get(url, headers = hdrs)
print(response)
info = response.json()
print(json.dumps(info, indent=4))

print(response) 给出 output 作为 '<Response [304]>' 而 info = response.json() 给出回溯错误 'Expecting value: line 1 column 1 (char 0)'

有人可以指出我正确的方向吗?

提前致谢!

发表评论作为答案:

为了检索数据,api 所需的标头是平台:ecoregistry。

import requests as req
import json
req = req.get('https://api-front.ecoregistry.io/api/project/10/emitcertifications', headers={'platform': 'ecoregistry'})
data = json.loads(data)
print(data.keys())
# dict_keys(['status', 'projectSerialYear', 'yearValidation', 'project', 'emitcertifications'])
print(data['emitcertifications'][0].keys())
# dict_keys(['id', 'auth', 'operation', 'typeRemoval', 'consecutive', 'serialInit', 'serialEnd', 'serial', 'passiveSubject', 'passiveSubjectNit', 'isPublicEndUser', 'isAccept', 'isCanceled', 'isCancelProccess', 'isUpdated', 'isKg', 'reasonUsingCarbonOffsetsId', 'reasonUsingCarbonOffsets', 'quantity', 'date', 'nitEnd', 'userEnd'])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM