首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在python中读取json文件后返回_default_decoder.decode错误

在python中读取json文件后返回_default_decoder.decode错误
EN

Stack Overflow用户
提问于 2022-08-18 20:46:36
回答 2查看 201关注 0票数 0
代码语言:javascript
运行
复制
url = 'https://www.magicbricks.com/property-for-sale/residential-real-estate?bedroom=&proptype=Multistorey-Apartment,Builder-Floor-Apartment,Penthouse,Studio-Apartment&cityName=Delhi-NCR'
result = requests.get(url)
data = result.json()

我正在尝试执行上面的代码,但是得到了以下错误。我是否需要导入其他模块或库,或者是否存在语法错误?我尝试过通过BeautifulSoup导入它,但是由于它是一个动态加载页面,所以它无法工作。

代码语言:javascript
运行
复制
---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
File ~\anaconda3\lib\site-packages\requests\models.py:910, in Response.json(self, **kwargs)
    909 try:
--> 910     return complexjson.loads(self.text, **kwargs)
    911 except JSONDecodeError as e:
    912     # Catch JSON-related errors and raise as requests.JSONDecodeError
    913     # This aliases json.JSONDecodeError and simplejson.JSONDecodeError

File ~\anaconda3\lib\json\__init__.py:346, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    343 if (cls is None and object_hook is None and
    344         parse_int is None and parse_float is None and
    345         parse_constant is None and object_pairs_hook is None and not kw):
--> 346     return _default_decoder.decode(s)
    347 if cls is None:

File ~\anaconda3\lib\json\decoder.py:337, in JSONDecoder.decode(self, s, _w)
    333 """Return the Python representation of ``s`` (a ``str`` instance
    334 containing a JSON document).
    335 
    336 """
--> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338 end = _w(s, end).end()

File ~\anaconda3\lib\json\decoder.py:355, in JSONDecoder.raw_decode(self, s, idx)
    354 except StopIteration as err:
--> 355     raise JSONDecodeError("Expecting value", s, err.value) from None
    356 return obj, end

JSONDecodeError: Expecting value: line 2 column 1 (char 1)

During handling of the above exception, another exception occurred:

JSONDecodeError                           Traceback (most recent call last)
Input In [14], in <cell line: 3>()
      1 url = 'https://www.magicbricks.com/property-for-sale/residential-real-estate?bedroom=&proptype=Multistorey-Apartment,Builder-Floor-Apartment,Penthouse,Studio-Apartment&cityName=Delhi-NCR'
      2 result = requests.get(url)
----> 3 data = result.json()

File ~\anaconda3\lib\site-packages\requests\models.py:917, in Response.json(self, **kwargs)
    915     raise RequestsJSONDecodeError(e.message)
    916 else:
--> 917     raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
EN

回答 2

Stack Overflow用户

发布于 2022-08-18 20:53:32

这是因为响应内容是html。做print(result.text),你就会看到:-D

检查不同的url是否会给您提供一个json,否则,您可能需要使用BeautifulSoup刮痕来解析html。

票数 0
EN

Stack Overflow用户

发布于 2022-08-18 21:38:32

该页面通过XHR调用从API中提取其结果(请查看Dev Network选项卡)。您需要抓取特定的端点,以获得json数据。有一种方法可以做到:

代码语言:javascript
运行
复制
import requests

url = 'https://www.magicbricks.com/mbsrp/propertySearch.html?editSearch=Y&category=S&propertyType=10002,10003,10021,10022&city=3327-1001011&page=2&groupstart=30&offset=0&maxOffset=13&sortBy=premiumRecent&pType=10002,10003,10021,10022&isNRI=Y'

r = requests.get(url)
print(r.json())

这将返回一个(相当大的) JSON响应:

代码语言:javascript
运行
复制
{'nsrResultList': None, 'resultList': [{'encId': 'VNJDDhBvWUJzpSvf+uAgZw==', 'possStatusD': 'Ready to Move', 'pmtSource': 'android', 'cntvl': '100', 'url': '2-BHK-940-Sq-ft-Multistorey-Apartment-FOR-Sale-K-R-Puram-in-Bangalore&id=4d423630333031323033', 'floorNo': '2', 'noBfCt': 0, 'st': 516, 'modstat': '20205', 'lCtDate': 1660792748000, 'appVer': 350, 'appSub': 'Y', 'deviceType': 'and', 'sendbirdId': '487111c6-5cb4-49e6-a35a-2dfa97bd6b62dipa6275', 'lastAccessDate': 1655404200000, 'projectSocietyLink': 'teja-nivas-k-r-puram-bangalore-pdpid-4d4235313830303531', 'sipro': '52063429 61161925 62018289 59395151', 'comFlag': 'N', 'appExc': 'N', 'cScore': '100', 'postDateT': '2022-08-11T23:54:46.000Z', 'endDateT': '2022-12-09T23:54:46.000Z', 'pl': 'Y', 'luxAmenities': '12223 12216', 'luxAmenitiesD': 'RO Water System,Visitor Parking', 'appovedAuthC': 'ABADev', 'nriPref': 'Y', 'enc_Id': 'VNJDDhBvWUKzvQzSxKfkaA==', 'cowDays': 0, 'price': 4200000, 'priceD': '42 Lac',....}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73409239

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档