网站建设资讯

NEWS

网站建设资讯

django-关于JSON编码格式的异常处理-创新互联

这个是编码格式的异常,因为在爬虫元服务的对接中,通过http发送数据的时候,打包下来的数据封装的格式不对

成都创新互联是一家专注于成都网站建设、网站设计与策划设计,滨江网站建设哪家好?成都创新互联做网站,专注于网站建设十年,网设计领域的专业建站公司;建站业务涵盖:滨江等地区。滨江做网站价格咨询:028-86922220
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)

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


名称栏目:django-关于JSON编码格式的异常处理-创新互联
标题来源:http://njwzjz.com/article/dhjggo.html