Search code examples
pythondictionarypycurl

extract data from a dictionary returned by pycurl


I have this:

import pycurl
import pprint
import json

c = pycurl.Curl()
c.setopt(c.URL, 'https://mydomainname.com')

c.perform()

the above code return a dictionary like this:

{"name":"steve", "lastvisit":"10-02-2012", "age":12}

I want to loop through that dictionary and get just the age:

age : 12

I tried:

diction = {}
diction = c.perform()
pprint.pprint(diction["age"])

No data returned and I got this error:

TypeError: 'NoneType' object is unsubscriptable

Solution

  • c.perform() doesn't return anything, you need to configure a file-like object to capture the value. A BytesIO object would do, you can then call .getvalue() on that after the call completes:

    import pycurl
    import pprint
    import json
    from io import BytesIO
    
    c = pycurl.Curl()
    data = BytesIO()
    
    c.setopt(c.URL, 'https://mydomainname.com')
    c.setopt(c.WRITEFUNCTION, data.write)
    c.perform()
    
    dictionary = json.loads(data.getvalue())
    pprint.pprint(dictionary["age"])
    

    If you are not married to pycurl, you might find requests to be a lot easier:

    import pprint
    import requests
    
    dictionary = requests.get('https://mydomainname.com').json()
    pprint.pprint(dictionary["age"])
    

    Even the standard library urllib.request module will be easier than using pycurl:

    from urllib.request import urlopen
    import pprint
    import json
    
    response = urlopen('https://mydomainname.com')
    dictionary = json.load(response)
    pprint.pprint(dictionary["age"])