I can't import urllib2

14,891

urllib2 was merged with urllib in python 3. Is a standard library in python 2 but not in 3.

Try this if you want to use the urlopen method

from urllib.request import urlopen
html = urlopen("http://www.google.com/")
print(html)

Also:

The urllib2 module has been split across several modules in Python 3.0 named urllib.request and urllib.error. The 2to3 tool will automatically adapt imports when converting your sources to 3

Share:
14,891
Himanshu Shankar
Author by

Himanshu Shankar

Updated on June 04, 2022

Comments

  • Himanshu Shankar
    Himanshu Shankar almost 2 years
    import urllib
    import urllib2
    url = 'http://localhost/ehaat/index.php/log-in'
    form_data = {'username': 'admin', 'password': 'admin'}
    params = urllib.urlencode(form_data)
    response = urllib2.urlopen(url, params)
    data = response.read()
    

    I am new to this urllib. So what is that I am doing wrong here? And also, can I use this to post data into a form (automating task of filling a form and click on submit) by taking random data's from files. I want to use this to test my website.

    Error:

    Traceback (most recent call last): File "D:\Users\iamhssingh\Documents\Python\url.py", line 2, in import urllib2 ImportError: No module named 'urllib2'

  • Himanshu Shankar
    Himanshu Shankar almost 9 years
    It does gives a response! Can you give me a code that how do I post data in a form using this? Or the thing that I am trying to do above?
  • Himanshu Shankar
    Himanshu Shankar almost 9 years
    Okay I will post another question!
  • Prerak Sola
    Prerak Sola almost 9 years
    @HimanshuShankar: And please don't ask Provide me the code question. First try it on your own and if you get any errors, come back here. We are here to help you out.
  • Himanshu Shankar
    Himanshu Shankar almost 9 years
    I have already searched a lot. And I had this answer too somewhere. But still I don't get it. How do I do what I am trying to do above using this? Now I get a error that urllib not present!
  • lapinkoira
    lapinkoira almost 9 years
    Here you have a similar question related to a POST with urllib in python 3 stackoverflow.com/questions/13288930/…