Python urllib2: Receive JSON response from url

后端 未结 10 2038
轮回少年
轮回少年 2020-11-29 17:22

I am trying to GET a URL using Python and the response is JSON. However, when I run

import urllib2
response = urllib2.urlopen(\'https://api.instagram.com/v1/         


        
10条回答
  •  佛祖请我去吃肉
    2020-11-29 17:57

    Though I guess it has already answered I would like to add my little bit in this

    import json
    import urllib2
    class Website(object):
        def __init__(self,name):
            self.name = name 
        def dump(self):
         self.data= urllib2.urlopen(self.name)
         return self.data
    
        def convJSON(self):
             data=  json.load(self.dump())
         print data
    
    domain = Website("https://example.com")
    domain.convJSON()
    

    Note : object passed to json.load() should support .read() , therefore urllib2.urlopen(self.name).read() would not work . Doamin passed should be provided with protocol in this case http

提交回复
热议问题