Search code examples
pythonpython-3.xurllib2urllib

Python 2 to Python 3 : TypeError: 'module' object is not callable


I'm trying to modify a code which was written in Python 2 Language with the urllib2 module.I did modified my code with the module urllib in Python 3 but I'm getting error :

req = urllib.request(url)

TypeError: 'module' object is not callable

What I am doing wrong here?

import urllib.request
import json
import datetime
import csv
import time

app_id = "172"
app_secret = "ce3" 


def testFacebookPageData(page_id, access_token):

    # construct the URL string
    base = "https://graph.facebook.com/v2.4"
    node = "/" + page_id
    parameters = "/?access_token=%s" % access_token
    url = base + node + parameters

    # retrieve data
    req = urllib.request(url)
    response = urllib.urlopen(req)
    data = json.loads(response.read())

    print (json.dumps(data, indent=4, sort_keys=True))

Solution

  • Change the lines

    req = urllib.request(url)
    response = urllib.urlopen(req)
    

    to:

    req = urllib.request.Request(url)
    response = urllib.request.urlopen(req)
    

    You can find more information on this module **https://docs.python.org/3/library/urllib.request.html#urllib.request.Request **https://docs.python.org/3/library/urllib.request.html#urllib.request.urlopen