opening a url with urllib in python 3

后端 未结 5 649
我在风中等你
我在风中等你 2020-12-24 07:27

i\'m trying to open the URL of this API from the sunlight foundation and return the data from the page in json. this is the code Ive produced, minus the parenthesis around m

5条回答
  •  感情败类
    2020-12-24 07:51

    The problem is with your module import. There are multiple ways to do that in Python3.x:

    from urllib.request import urlopen
    urlopen('example.com/***')
    

    or

    from urllib import request
    request.urlopen('example.com/***')
    

    or

    import urllib
    urllib.request.urlopen('example.com/***')
    

    Moreover, you can name the module you want to import as well:

    import urllib.request as req  ## or whatever you want
    req.urlopen('example.com/***')
    

    You can use the one you prefer, but watch the sequence of modules and packages you are using.

提交回复
热议问题