django-关于JSON编码格式的异常处理
这个是编码格式的异常,因为在爬虫元服务的对接中,通过http发送数据的时候,打包下来的数据封装的格式不对
在马关等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都做网站、网站制作 网站设计制作按需求定制制作,公司网站建设,企业网站建设,高端网站设计,营销型网站,成都外贸网站建设,马关网站建设费用合理。
string={'domain': 'android.myapp.com', 'url': 'https://android.myapp.com/myapp/searchAjax.htm?kw=MM%E5%BA%94%E7%94%A8%E5%95%86%E5%9C%BA&pns=&sid='}
因为JSON中并没有单引号,仅有双引号,所以我们需要把单引号替换成双引号了
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes
方式有多种,比如用正则
data = re.sub('\'', '\"', string.decode("utf-8"))
还可以同简单的字符串处理
string.replace('\"', '\'')
以下是系统的异常抛出
Traceback (most recent call last):
File "/home/yerikyu/spidermax/lib/python3.5/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/yerikyu/spidermax/lib/python3.5/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/yerikyu/spidermax/lib/python3.5/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/yerikyu/spidermax/lib/python3.5/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/yerikyu/spidermax/lib/python3.5/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/home/yerikyu/SG/spidermax/spider/views.py", line 43, in post
data = json.loads(request.body.decode("utf-8"))
File "/usr/lib/python3.5/json/__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.5/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.5/json/decoder.py", line 355, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
本文名称:django-关于JSON编码格式的异常处理
文章URL:http://ybzwz.com/article/ghpogj.html