我无法导入 urllib2

I can't import urllib2

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()

我是这个 urllib 的新手。那么我在这里做错了什么?而且,我可以通过从文件中获取随机数据来使用它 post 数据到表单中(自动完成填写表单并单击提交的任务)。我想用它来测试我的网站。

错误:

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

urllib2 在 python 3 中与 urllib 合并。在 python 中是标准库 2 但在 3 中不是。

如果你想使用 urlopen 方法,试试这个

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

另外:

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