本文整理汇总了Python中future.backports.urllib.request方法的典型用法代码示例。如果您正苦于以下问题:Python urllib.request方法的具体用法?Python urllib.request怎么用?Python urllib.request使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类future.backports.urllib
的用法示例。
在下文中一共展示了urllib.request方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: from_import
# 需要导入模块: from future.backports import urllib [as 别名]
# 或者: from future.backports.urllib import request [as 别名]
def from_import(module_name, *symbol_names, **kwargs):
"""
Example use:
>>> HTTPConnection = from_import('http.client', 'HTTPConnection')
>>> HTTPServer = from_import('http.server', 'HTTPServer')
>>> urlopen, urlparse = from_import('urllib.request', 'urlopen', 'urlparse')
Equivalent to this on Py3:
>>> from module_name import symbol_names[0], symbol_names[1], ...
and this on Py2:
>>> from future.moves.module_name import symbol_names[0], ...
or:
>>> from future.backports.module_name import symbol_names[0], ...
except that it also handles dotted module names such as ``http.client``.
"""
if PY3:
return __import__(module_name)
else:
if 'backport' in kwargs and bool(kwargs['backport']):
prefix = 'future.backports'
else:
prefix = 'future.moves'
parts = prefix.split('.') + module_name.split('.')
module = importlib.import_module(prefix + '.' + module_name)
output = [getattr(module, name) for name in symbol_names]
if len(output) == 1:
return output[0]
else:
return output
示例2: read
# 需要导入模块: from future.backports import urllib [as 别名]
# 或者: from future.backports.urllib import request [as 别名]
def read(self):
"""Reads the robots.txt URL and feeds it to the parser."""
try:
f = urllib.request.urlopen(self.url)
except urllib.error.HTTPError as err:
if err.code in (401, 403):
self.disallow_all = True
elif err.code >= 400:
self.allow_all = True
else:
raw = f.read()
self.parse(raw.decode("utf-8").splitlines())