[英]How to select data from JSON to Excel with Pandas
晚上好!
我正在處理一個項目,我想從網站中提取 JSON 數據,然后將其導入 Excel/CSV 文件。 我是 web 用 Selenium 抓取網頁,並使用 JSON 到 json_loads 和 json_normalize。 當我使用 json_normalize function 時,打印出來時不會出現所有數據。 我想做的是 select 一些數據,讓它看起來不錯。
來自網站的 JSON 數據:
{
"getUrl": "/395012/Organization/pase10001",
"className": "Organization",
"data": {
"name": "ICA Supermarket",
"organizationNumber": "556589-4341",
"centralPhoneNumber": {
"value": "044-310010",
"normalized": "+4644310010",
"className": "PhoneNumber",
"isEmpty": false
},
"faxPhoneNumber": {
"value": null,
"normalized": null,
"className": "PhoneNumber",
"isEmpty": true
},
"website": "www.ica.se",
"email": {
"value": "kundkontakt.tollarp@supermarket.ica.se",
"className": "Email"
},
"dateLastModified": "/Date(1621342946134+0200)/",
"visitAddress": {
"street": "Polgatan 5",
"zipCode": "298 32",
"city": "TOLLARP",
"countryCode": ""
},
"postalAddress": {
"street": "Box 24",
"zipCode": "298 21",
"city": "TOLLARP",
"countryCode": ""
},
"responsibleCoworker": null,
"integrationid": "",
"customFields": [],
"relation": 0,
"tags": [],
"headOffice": null,
"corporateGroup": null,
"sharedBody": {
"vatNumber": "SE556589434101",
"lineOfBusiness": "Livsmedelshandel med brett sortiment, ej varuhus eller stormarknad",
"businessDescription": "Bolaget skall som medlem i ICA-förbundet bedriva detaljhandelsrörelse med dagligvaror och annan därmed förenlig verksamhet.",
"legalForm": "Aktiebolag",
"dateOfRegistration": "2000-04-06",
"legalName": "Superlivs i Tollarp AB",
"rating": null,
"numberOfSubsidaries": 0,
"numberOfEmployeesRange": "20 - 49",
"numberOfEmployeesWorkSite": "20 - 49"
我想 select 這個 JSON 數據的某些信息並將其導出到 Excel 文檔。 我希望 Excel 具有此順序的類別,例如:
公司 | 組織 |
---|---|
名稱1 | 000000000000 |
到目前為止我已經嘗試過:
res = self.driver.find_element_by_tag_name("pre").text
data = json.loads(res)
xd = pd.json_normalize(data])
xd.to_excel("output.xlsx")
我是 Python 的新手,並嘗試盡可能多地學習。 如果你能幫助我如何在這個項目中走得更遠,那真的會讓我很開心!
您也可以嘗試convtools
庫,這將幫助您動態構建轉換器。 備忘單在這里。
import json
from convtools import conversion as c
data = json.loads(res)
converter = c.list_comp({
"company": c.item("data", "name"),
"org_number": c.item("data", "organizationNumber"),
}).gen_converter(debug=True) # install "black" to see formatted sources
prepared_data = converter([json.loads(res)])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.