日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python遇坑記錄-json.loads

系統(tǒng) 1764 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):
        """我出現(xiàn)JSONDecodeError的代碼"""
        url = "https://dealer.autohome.com.cn/Ajax/GetDealerInfo?DealerId=2052112"

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

        # 將json字符串轉(zhuǎn)為python的字典
        body = json.loads(response)
        print(body, '\n', type(body))

        error_json_data_example = '{"Address":"北侖區(qū)新?\街道明州西路555號(hào)1幢1號(hào)-1"}'

    def my_problem_and_solution(self):
        """我的問題所在及解決方式,是request返回?cái)?shù)據(jù)的編碼解碼問題,主要在這個(gè)生僻漢字'碶'上"""
        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字符串轉(zhuǎn)為python的字典
        body = json.loads(response)
        print(body, '\n', type(body))


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

            
          

運(yùn)行以上代碼:

            
              解決后
{'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': '寧波寶利行豐田汽車銷售服務(wù)有限公司', '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': '寧波北侖區(qū)新碶街道大港中路29號(hào)1幢1號(hào)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)
                
              
            
          

問題解決。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 潮州市| 宁晋县| 上饶市| 丹寨县| 郧西县| 肥东县| 佳木斯市| 和田县| 桐城市| 常州市| 怀化市| 桓台县| 阿克苏市| 保靖县| 嵊泗县| 峨眉山市| 瓦房店市| 松潘县| 永定县| 临泽县| 灯塔市| 田林县| 武威市| 东乌珠穆沁旗| 公安县| 石城县| 寿阳县| 宜阳县| 新干县| 林州市| 抚远县| 宜兴市| 乳源| 马边| 郓城县| 南通市| 永德县| 泾源县| 驻马店市| 马边| 泰来县|