【发布时间】:2011-11-28 21:07:59
【问题描述】:
我在使用 Python 的文档时遇到了很多麻烦。有没有类似 Mozilla 开发者网络的东西?
我正在做一个 Python 拼图网站,我需要能够阅读页面的内容。我在一个网站上看到了以下内容:
import urllib2
urlStr = 'http://www.python.org/'
try:
fileHandle = urllib2.urlopen(urlStr)
str1 = fileHandle.read()
fileHandle.close()
print ('-'*50)
print ('HTML code of URL =', urlStr)
print ('-'*50)
except IOError:
print ('Cannot open URL %s for reading' % urlStr)
str1 = 'error!'
print (str1)
它一直说没有 urllib2 模块。
Python 文档说
The urllib module has been split into parts and renamed in Python 3.0 to urllib.request, urllib.parse, and urllib.error. The 2to3 tool will automatically adapt imports when converting your sources to 3.0. Also note that the urllib.urlopen() function has been removed in Python 3.0 in favor of urllib2.urlopen().
我也尝试导入 urllib.request,但它说 urllib 2 已定义... WTF 在这里发生吗?
版本 3.2.2
【问题讨论】:
-
此时您的 Python 版本会很有用。
-
已更新。除了给定的内容之外,还有其他 Python 文档吗?
-
@Walkerneo: docs.python.org/py3k/library/urllib.request.html
-
这是您使用的文档吗? docs.python.org/py3k
-
是的,是的。我一直在用那个。 @icktoofay 谢谢,这有帮助。那我必须做 urllib.request.openurl 。谢谢,把它作为答案,我会接受它。