I can't import urllib2

15.6k views Asked by At
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'

1

There are 1 answers

5
lapinkoira On BEST ANSWER

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