简体   繁体   English

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

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

I am trying to extract the data in the table at https://www.ecoregistry.io/emit-certifications/ra/10我正在尝试提取表中的数据https://www.ecoregistry.io/emit-certifications/ra/10

Using the google developer tools.network tab, I am able to get the json link where the data for this table is stored: https://api-front.ecoregistry.io/api/project/10/emitcertifications使用 google developer tools.network 选项卡,我可以获得存储此表数据的 json 链接: https://api-front.ecoregistry.io/api/project/10/emitcertifications

I am able to manually copy this json data and extract the information using this code I've written:我可以手动复制这个 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')

I want to automate it such that I can extract the data from the json link: https://api-front.ecoregistry.io/api/project/10/emitcertifications directly instead of manually pasting json data in:我想自动化它,以便我可以从 json 链接中提取数据: https://api-front.ecoregistry.io/api/project/10/emitcertifications直接而不是手动粘贴 json 数据:

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

The link is not working in python or even in browser directly:该链接在 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))

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

When I download the data from the developer tools then this dictionary has 'status':1 and after that all the data is there.当我从开发人员工具下载数据时,这本字典有 'status':1 之后所有数据都在那里。

Edit: I tried adding request headers to the url but it still did not work:编辑:我尝试将请求标头添加到 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) give output as '<Response [304]>' while info = response.json() gives traceback error 'Expecting value: line 1 column 1 (char 0)' print(response) 给出 output 作为 '<Response [304]>' 而 info = response.json() 给出回溯错误 'Expecting value: line 1 column 1 (char 0)'

Can someone please point me in the right direction?有人可以指出我正确的方向吗?

Thanks in advance!提前致谢!

Posting comment as an answer:发表评论作为答案:

The headers required for that api in order to retrieve data is platform: ecoregistry.为了检索数据,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.

相关问题 如何使用浏览器开发人员工具从 POST 方法获取数据 - How to get data from POST method using browser developer tools 我可以使用 python 访问谷歌浏览器开发者工具的网络选项卡吗? - can I access to network tab of google chrome developer tools with python? 如何使用 python selenium 访问 google chrome 开发人员工具上的安全面板? - How to access Security panel on google chrome developer tools with python selenium? Python 请求标头不起作用 - 检查 Chrome 开发者工具 -&gt; 网络 - Python requests header not working - Checked Chrome developer tools -> Network 使用Google App Engine Python的最佳工具 - The best tools for working with Google App Engine Python 为什么在浏览器开发者工具中获取的数据与 BeautifulSoap / Postman 不同? - Why getting different data in browser developer tools vs BeautifulSoap / Postman? JSON无法在Google财经中使用 - JSON not working from Google Finance python selenium如何从谷歌获取链接 - python selenium how to get link from google Python selenium 获取“开发者工具”→网络→媒体日志 - Python selenium get "Developer Tools" →Network→Media logs Cookie未显示在Chrome开发者工具(Django Python)中 - Cookie does not show up in Chrome Developer Tools (Django Python)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM