本文整理汇总了Python中w3lib.encoding.resolve_encoding函数的典型用法代码示例。如果您正苦于以下问题:Python resolve_encoding函数的具体用法?Python resolve_encoding怎么用?Python resolve_encoding使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了resolve_encoding函数的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: _auto_detect_fun
def _auto_detect_fun(self, text):
for enc in (self._DEFAULT_ENCODING, 'utf-8', 'cp1252'):
try:
text.decode(enc)
except UnicodeError:
continue
return resolve_encoding(enc)
开发者ID:AugustLONG,项目名称:scrapy,代码行数:7,代码来源:text.py
示例2: _auto_detect_fun
def _auto_detect_fun(self, text):
for enc in (self._DEFAULT_ENCODING, 'utf-8', 'ascii', 'GB18030'):
try:
text.decode(enc)
except UnicodeError:
continue
return resolve_encoding(enc)
#detect by chardet by wsy
cc = chardet.detect(text)
if cc is None:
return
enc = cc.get('encoding', None)
if enc is None:
return
try:
text.decode(enc)
except:
return None
else:
return resolve_encoding(enc)
开发者ID:wusy1209,项目名称:scrapy,代码行数:21,代码来源:text.py
示例3: test_process_response_no_content_type_header
def test_process_response_no_content_type_header(self):
headers = {
'Content-Encoding': 'identity',
}
plainbody = b"""<html><head><title>Some page</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312">"""
respcls = responsetypes.from_args(url="http://www.example.com/index", headers=headers, body=plainbody)
response = respcls("http://www.example.com/index", headers=headers, body=plainbody)
request = Request("http://www.example.com/index")
newresponse = self.mw.process_response(request, response, self.spider)
assert isinstance(newresponse, respcls)
self.assertEqual(newresponse.body, plainbody)
self.assertEqual(newresponse.encoding, resolve_encoding('gb2312'))
开发者ID:ArturGaspar,项目名称:scrapy,代码行数:13,代码来源:test_downloadermiddleware_httpcompression.py
示例4: test_process_response_force_recalculate_encoding
def test_process_response_force_recalculate_encoding(self):
headers = {
'Content-Type': 'text/html',
'Content-Encoding': 'gzip',
}
f = BytesIO()
plainbody = b"""<html><head><title>Some page</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312">"""
zf = GzipFile(fileobj=f, mode='wb')
zf.write(plainbody)
zf.close()
response = HtmlResponse("http;//www.example.com/page.html", headers=headers, body=f.getvalue())
request = Request("http://www.example.com/")
newresponse = self.mw.process_response(request, response, self.spider)
assert isinstance(newresponse, HtmlResponse)
self.assertEqual(newresponse.body, plainbody)
self.assertEqual(newresponse.encoding, resolve_encoding('gb2312'))
开发者ID:BillWangCS,项目名称:scrapy,代码行数:17,代码来源:test_downloadermiddleware_httpcompression.py
示例5: _assert_response_encoding
def _assert_response_encoding(self, response, encoding):
self.assertEqual(response.encoding, resolve_encoding(encoding))
开发者ID:elkingtowa,项目名称:pyrake,代码行数:2,代码来源:test_http_response.py
示例6: test_resolve_encoding
def test_resolve_encoding(self):
self.assertEqual(resolve_encoding('latin1'), 'cp1252')
self.assertEqual(resolve_encoding(' Latin-1'), 'cp1252')
self.assertEqual(resolve_encoding('gb_2312-80'), 'gb18030')
self.assertEqual(resolve_encoding('unknown encoding'), None)
开发者ID:Dior222,项目名称:w3lib,代码行数:5,代码来源:test_encoding.py
注:本文中的w3lib.encoding.resolve_encoding函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论