简体   繁体   English

如何从HTTP响应中提取JSON数据并将其存储在python中的excel /文本文件中

[英]How to scrap JSON data from HTTP response and store it in excel/text file with python

How I can scrap JSON data from following response and save it in excel/text file with python ? 我如何从以下响应中抓取JSON数据并将其保存在python中的excel / text文件中?

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-Powered-By: Servlet 2.5; JBoss-5.0/JBossWeb-2.1
Content-Disposition: attachment
Content-Type: application/json;charset=utf-8
Content-Length: 797
Date: Sun, 06 Nov 2016 08:32:13 GMT
Via: HTTP/1.1 sophos.http.proxy:3128
Connection: keep-alive

//OK[3,3,2,3,1,["java.util.ArrayList/4159755760","123456789","Bruce Wayne","Male","John","NO","1303052","bruceWayne@gmail.com","123456789","A+","NO","No","00","American","63005541113","ssc.client.PersonalDetailValuesManager/227521317","20/05/1996","California","445302","USA"],0,7]

I want to scrap data such as 123456789, Bruce Wayne, Email, etc. in excel/text file, but how I can do it with python? 我想在excel /文本文件中抓取123456789,Bruce Wayne,Email等数据,但是如何使用python呢? (If not in any programming language) (如果没有任何编程语言)

For Python3 use, urllib.request , json and xlsxwriter 对于Python3使用, urllib.requestjsonxlsxwriter

import urllib.request
import json

#read the JSON response
req = urllib.request.urlopen('http://headers.jsontest.com/')
answer = req.read()
answer_json = json.loads(answer.decode())

#save as a text file
with open('json_dump.txt', 'w') as f:
    json.dump(answer_json, f)

#now write to Excel
import xlsxwriter

workbook = xlsxwriter.Workbook('json_dump.xlsx')
worksheet = workbook.add_worksheet()

row = 0
col = 0

for key in answer_json.keys():
    worksheet.write(row, col, key)
    worksheet.write(row, col + 1, answer_json[key])
    row += 1
workbook.close()

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

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