无法从包含json数据的网页获取有效回复

时间:2018-01-26 22:04:41

标签: python json python-3.x web-scraping

我在python中编写了一个脚本来获取网页的响应。该网页中的数据采用json格式。但是,当我尝试如下,我得到一个错误。有人可以告诉我如何获得有效的答复吗?

这是我的失败尝试:

import requests
import json

URL = "https://www.sandicormls.com/agent/AgentSearch?officeSortOption=name&_=1516998894917&_keywordsAll=&officeLocation=&sortOption=rndsrt&_keywordsAgentName=&page=&typeIn=Realtor%2CBroker%2COwner%2COffice+Manager%2CAppraiser&searchMode=agent&officeName="
res = requests.get(URL,headers={'User-Agent':'Mozilla/5.0'})
print(res.json())

这是追溯:

Traceback (most recent call last):
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\new_line_one.py", line 34, in <module>
    print(res.json())
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests\models.py", line 892, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\json\__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\json\decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\json\decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

1 个答案:

答案 0 :(得分:0)

我刚试了一下,确认你的代码很好,因为它适用于我的环境。虽然,我在Linux(Ubuntu 16.04)虚拟机上运行它。 您可以查看要返回的数据

print(res.headers[‘content-type’])

或检查

等内容
print(res.text)