urllib “module object is not callable”

后端 未结 3 1539
长情又很酷
长情又很酷 2020-12-11 05:55

This is my third python project, and I\'ve received an error message: \'module object\' is not callable.

I know that this means I\'m referencing a varia

相关标签:
3条回答
  • 2020-12-11 06:26

    It also occurs if you have declared the returning method as a property method by annotating with @property.

    0 讨论(0)
  • 2020-12-11 06:29

    urllib.request is a module. urllib.request.Request is a class. Calling a module like you're currently doing raises an error. You probably want to call the class, like this:

    request = urllib.request.Request(url, headers=req_headers)  # create a request object for the URL
    

    You'll also probably want to use build_opener of urllib.request rather than just urllib:

    opener = urllib.request.build_opener()  # create an opener object
    
    0 讨论(0)
  • 2020-12-11 06:30

    In python 3, the urllib.request object is a module. You need to call objects contained in this module. This is an important change from Python 2, if you are using example code you need to take that into account.

    For example, creating the Request object and the opener:

    request = urllib.request.Request(url, headers=req_headers)
    opener = urllib.request.build_opener()
    response = opener.open(request)
    

    Read the documentation carefully.

    0 讨论(0)
提交回复
热议问题