当前位置: 首页>>代码示例>>Python>>正文


Python FancyURLopener.addheader方法代码示例

本文整理汇总了Python中urllib.FancyURLopener.addheader方法的典型用法代码示例。如果您正苦于以下问题:Python FancyURLopener.addheader方法的具体用法?Python FancyURLopener.addheader怎么用?Python FancyURLopener.addheader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在urllib.FancyURLopener的用法示例。


在下文中一共展示了FancyURLopener.addheader方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: unicode_urlopen

# 需要导入模块: from urllib import FancyURLopener [as 别名]
# 或者: from urllib.FancyURLopener import addheader [as 别名]
def unicode_urlopen(url, accept_lang=None):
    """Returns a *Unicode* file-like object for non-local documents.
    Client must ensure that the URL points to non-binary data. Pass in
    an Accept-Language value to configure the FancyURLopener we
    use."""

    opener = FancyURLopener()

    if accept_lang:
        opener.addheader("Accept-Language", accept_lang)

    # We want to convert the bytes file-like object returned by
    # urllib, which is bytes in both Python 2 and Python 3
    # fortunately, and turn it into a Unicode file-like object
    # with a little help from our StringIO friend.
    page = opener.open(url)
    encoding = page.headers['content-type']
    encoding = encoding.split('charset=')
    if len(encoding) > 1:
        encoding = encoding[-1]
        page = page.read().decode(encoding)
    else:
        page = page.read()
        encoding = meta_encoding(page) or 'utf8'
        page = page.decode(encoding)

    page = StringIO(page)
    return page
开发者ID:Mekyi,项目名称:crunchy,代码行数:30,代码来源:utilities.py

示例2: get

# 需要导入模块: from urllib import FancyURLopener [as 别名]
# 或者: from urllib.FancyURLopener import addheader [as 别名]
 def get(self, url, headers=None):
     o = FancyURLopener()
     if headers:
         for k, v in headers.items():
             o.addheader(k, v)
     self.req = o.open(url)
     return self
开发者ID:cowlicks,项目名称:rio,代码行数:9,代码来源:streamer.py

示例3: __load_page

# 需要导入模块: from urllib import FancyURLopener [as 别名]
# 或者: from urllib.FancyURLopener import addheader [as 别名]
	def __load_page(self, url):
		res = None
		body = None
		opener = FancyURLopener()

		# Clear default User-Agent header which is defined in addheaders.
		opener.addheaders = []
		for key, value in request_headers.iteritems():
			opener.addheader(key, value)
		opener.addheader("Cookie", self.cookie)

		try:
			res = opener.open(url)
			body = res.read()
		except IOError, error:
			logging.error(error.strerror)
开发者ID:x-cray,项目名称:votingstats,代码行数:18,代码来源:grab.py

示例4: fetchURL

# 需要导入模块: from urllib import FancyURLopener [as 别名]
# 或者: from urllib.FancyURLopener import addheader [as 别名]
def fetchURL(url, file='', params=None, headers={}, isBinary=False, encodeURL=True):
	log("> bbbLib.fetchURL() %s isBinary=%s encodeURL=%s" % (url, isBinary, encodeURL))
	if encodeURL:
		safe_url = quote_plus(url,'/:&?=+#@')
	else:
		safe_url = url

	success = False
	data = None
	if not file:
		# create temp file if needed
		file = xbmc.translatePath(os.path.join(os.getcwd(), "temp.html"))

	# remove destination file if exists already
	deleteFile(file)

	# fetch from url
	try:
		opener = FancyURLopener()

		# add headers if supplied
#		if headers:
		if not headers.has_key('User-Agent')  and not headers.has_key('User-agent'):
			headers['User-Agent'] = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
		for name, value  in headers.items():
			opener.addheader(name, value)

		fn, resp = opener.retrieve(safe_url, file, data=params)
#		print fn, resp

		content_type = resp.get("Content-Type",'').lower()
		# fail if expecting an image but not corrent type returned
		if isBinary and (find(content_type,"text") != -1):
			raise "Not Binary"

		opener.close()
		del opener
		urlcleanup()
	except IOError, errobj:
		ErrorCode(errobj)
开发者ID:drrlramsey,项目名称:xbmc-addons,代码行数:42,代码来源:bbbLib.py


注:本文中的urllib.FancyURLopener.addheader方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。