我正在嘗試在https://www.ecoregistry.io/emit-certifications/ra/10的表格中提取資料
使用谷歌開發人員工具>網路選項卡,我可以獲得存盤此表資料的 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))
我得到的錯誤輸出是: {'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) 給出輸出為 '<Response [304]>' 而 info = response.json() 給出回溯錯誤 'Expecting value: line 1 column 1 (char 0)'
有人可以指出我正確的方向嗎?
提前致謝!
uj5u.com熱心網友回復:
發表評論作為答案:
該 api 檢索資料所需的標頭是平臺:生態注冊。
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'])
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/468752.html
上一篇:Blazor模板的csproj中的SupportedPlatform
下一篇:將一些評論的評分刮成圖片
