python遇坑记录-json.loads

系统 1830 0
            
              # -*- coding=utf-8 -*-
import json

import requests

header = {
    'User-Agent': 'googlespider',
    'Content-Encoding': 'gzip',
    'X-Forwarded-For': '202.101.43.22',
}


class Business(object):
    def __init__(self):
        pass

    def my_JSONDecodeError(self):
        """我出现JSONDecodeError的代码"""
        url = "https://dealer.autohome.com.cn/Ajax/GetDealerInfo?DealerId=2052112"

        r = requests.get(url=url, headers=header)
        response = r.text

        # 将json字符串转为python的字典
        body = json.loads(response)
        print(body, '\n', type(body))

        error_json_data_example = '{"Address":"北仑区新�\街道明州西路555号1幢1号-1"}'

    def my_problem_and_solution(self):
        """我的问题所在及解决方式,是request返回数据的编码解码问题,主要在这个生僻汉字'碶'上"""
        url = "https://dealer.autohome.com.cn/Ajax/GetDealerInfo?DealerId=2052112"

        r = requests.get(url=url, headers=header)
        try:
            response = r.content.decode('utf-8')
        except UnicodeDecodeError:
            response = r.content.decode('GB18030')

        # 将json字符串转为python的字典
        body = json.loads(response)
        print(body, '\n', type(body))


if __name__ == "__main__":
    b = Business()
    
    # 解决后
    b.my_problem_and_solution()
    
    # 解决前
    b.my_JSONDecodeError()

            
          

运行以上代码:

            
              解决后
{'DealerId': 2052112, 'DealerInfoId': 2052106, 'MinistieName': None, 'MinistieSimpleName': None, 'KindId': 1, 'Is24h': True, 'IsAuth': False, 'IsCurrent': False, 'SiteTemplateID': 0, 'BsnsLcncCmpynName': None, 'BsnsLcncExpire': None, 'BsnsLcncNo': None, 'ContractType': 0, 'MaintainState': 0, 'IsCPL': False, 'PayType': 16, 'IsCPL_2019': False, 'MainBrandImgUrl': None, 'BrandName': None, 'BrandsNameString': None, 'BusinessArea': '售本市', 'GroupHotSeriesModel': None, 'HasCloseTestDriver': False, 'HasLowerPrice': False, 'HasUseVerificationCode': False, 'LeadsRangeType': 0, 'LeadsRangeTypeTittle': None, 'Phone_400': None, 'refStylePhone': None, 'SellPhone': '4008307068', 'ServicePhone': '0574-86968822', 'RescuePhone': None, 'IconStyle': None, 'AcceptCIds': None, 'AcceptPIds': None, 'MapLatBaidu': 29.915025, 'MapLonBaidu': 121.821191, 'LeadsRatingScore': 1, 'CallRate400': 1.0, 'StarLevel': 4, 'MainBrands': None, 'DealerType': 0, 'CompanyId': 0, 'CompanyName': None, 'Company': '宁波宝利行丰田汽车销售服务有限公司', 'CompanySimpleName': None, 'CompanySimple': '宁波宝利行', 'CID': 330200, 'CityName': '宁波', 'CityPinyin': None, 'SID': 330206, 'CountyName': None, 'PID': 330000, 'ProvinceName': None, 'GroupID': 0, 'GroupSimpleName': None, 'HasCloseOrder': False, 'CompanyDesc': None, 'Address': '宁波北仑区新碶街道大港中路29号1幢1号1层-3', 'Fax': None, 'CompanyURL': None} 
 
              
                
解决前
Traceback (most recent call last):
  File "/mac/project/webcrawler/service/analysis/boker.py", line 52, in 
                
                  
    b.my_JSONDecodeError()
  File "/mac/project/webcrawler/service/analysis/boker.py", line 25, in my_JSONDecodeError
    body = json.loads(response)
  File "/mac/anaconda3/lib/python3.6/json/__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "/mac/anaconda3/lib/python3.6/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/mac/anaconda3/lib/python3.6/json/decoder.py", line 355, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Invalid \escape: line 1 column 1169 (char 1168)
                
              
            
          

问题解决。


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论