python - urllib 上缺少方法

标签 python python-3.x urllib

为什么在我的 Python 3.4 安装上无法使用 urllib

我可以导入它,但我无法使用任何方法:

Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:25:23) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import urllib
>>> dir(urllib)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']

编辑:

我也看不到子模块:

>>> dir(urllib.request)
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    dir(urllib.request)
AttributeError: 'module' object has no attribute 'request'
>>> 

最佳答案

dir 误导了您。 Python 3.x 中有几个 urllib 子模块:https://docs.python.org/3.4/library/urllib.html

>>> import urllib
>>> dir(urllib)
['__builtins__', '__cached__', '__doc__', '__file__', '__name__', '__package__', '__path__']
>>> import urllib.request
>>> dir(urllib.request)
['AbstractBasicAuthHandler', 'AbstractDigestAuthHandler', 'AbstractHTTPHandler', 'BaseHandler', 'CacheFTPHandler', 'ContentTooShortError', 'FTPHandler', 'FancyURLopener', 'FileHandler', 'HTTPBasicAuthHandler', 'HTTPCookieProcessor', 'HTTPDefaultErrorHandler', 'HTTPDigestAuthHandler', 'HTTPError', 'HTTPErrorProcessor', 'HTTPHandler', 'HTTPPasswordMgr', 'HTTPPasswordMgrWithDefaultRealm', 'HTTPRedirectHandler', 'HTTPSHandler', 'MAXFTPCACHE', 'OpenerDirector', 'ProxyBasicAuthHandler', 'ProxyDigestAuthHandler', 'ProxyHandler', 'Request', 'URLError', 'URLopener', 'UnknownHandler', '__builtins__', '__cached__', '__doc__', '__file__', '__name__', '__package__', '__version__', '_cut_port_re', '_ftperrors', '_have_ssl', '_localhost', '_noheaders', '_opener', '_parse_proxy', '_proxy_bypass_macosx_sysconf', '_safe_gethostbyname', '_thishost', '_urlopener', 'addclosehook', 'addinfourl', 'base64', 'bisect', 'build_opener', 'collections', 'email', 'ftpcache', 'ftperrors', 'ftpwrapper', 'getproxies', 'getproxies_environment', 'hashlib', 'http', 'install_opener', 'io', 'localhost', 'noheaders', 'os', 'parse_http_list', 'parse_keqv_list', 'pathname2url', 'posixpath', 'proxy_bypass', 'proxy_bypass_environment', 'quote', 'random', 'randombytes', 're', 'request_host', 'socket', 'splitattr', 'splithost', 'splitpasswd', 'splitport', 'splitquery', 'splittag', 'splittype', 'splituser', 'splitvalue', 'ssl', 'sys', 'thishost', 'time', 'to_bytes', 'unquote', 'unwrap', 'url2pathname', 'urlcleanup', 'urljoin', 'urlopen', 'urlparse', 'urlretrieve', 'urlsplit', 'urlunparse']

关于python - urllib 上缺少方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23663188/

相关文章:

python - 使用 pytest 和 pytest-mock 模拟整个包

Python:检查命名管道是否有数据

Python:检查两个变量之间相同类型的最佳方法

python - 当状态为引发异常的 400 之类的错误时,如何读取 Python urllib 上的响应主体?

python - URLLib2.URL错误: Reading Server Response Codes (Python)

python - 页面抓取以从谷歌财经获取价格

php - 收集 subreddit 标题(批量)的最佳方式是什么

python - 从 UDP 中刮掉 scapy 负载的 8 个字节

python - 使用 asyncio 有两个无限任务

python - Pandas 在高于 0.21.0 的版本上使用 fillna() 和 sum() 返回不同的结果