本文整理匯總了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())