简体   繁体   中英

Saving json response in python

I am using this code do get information from my website with python, and this works like a charm, but can i save this output to a variable or at least to a json or txt file

import pycurl
def getUserData():
   c = pycurl.Curl()
   c.setopt(pycurl.URL, 'https://api.github.com/users/braitsch')
   c.setopt(pycurl.HTTPHEADER, ['Accept: application/json'])
   c.setopt(pycurl.VERBOSE, 0)
   c.setopt(pycurl.USERPWD, 'username:userpass')
   c.perform()
getUserData()

Don't use curl here; Python comes with batteries included, albeit with an API that is only marginally better than pycurl .

I recommend you install requests rather than try and make urllib2 and passwords work:

import requests

url = 'https://api.github.com/users/braitsch'
headers = {'Accept': 'application/json'}
auth = ('username', 'userpass')
response = requests.get(url, headers=headers, auth=auth)

with open('outputfile.json', 'wb') as outf:
    outf.write(response.content)

If the response is large, you can stream the content to a file, see How to download image using requests , the same techniques would apply here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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