3
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'

JJIqbal
  • 630
  • 1
  • 8
  • 23
Himanshu Shankar
  • 96
  • 1
  • 2
  • 8

1 Answers1

10

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

lapinkoira
  • 8,320
  • 9
  • 51
  • 94
  • 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 Jun 12 '15 at 07:47
  • Okay I will post another question! – Himanshu Shankar Jun 12 '15 at 07:51
  • @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. – Prerak Sola Jun 12 '15 at 07:53
  • 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! – Himanshu Shankar Jun 12 '15 at 07:54
  • Here you have a similar question related to a POST with urllib in python 3 http://stackoverflow.com/questions/13288930/how-to-pass-parameter-to-url-with-python-urlopen – lapinkoira Jun 12 '15 at 07:57