# -*- 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號1幢1號-1"}'
def my_problem_and_solution(self):
"""我的問題所在及解決方式,是request返回數(shù)據(jù)的編碼解碼問題,主要在這個生僻漢字'碶'上"""
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()
運行以上代碼:
解決后
{'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號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)
問題解決。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
