无法从包含json数据的网页获得有效响应

时间:2022-10-08 09:02:15

I've written a script in python to get response from a webpage. The data in that webpage are in json format. However, when I try like below I get an error. Can somebody give me any workaround as to how I can get a valid response?

我在python中编写了一个脚本来获取网页的响应。该网页中的数据采用json格式。但是,当我尝试下面的时候,我得到一个错误。有人可以给我任何关于如何得到有效回复的解决方法吗?

Here is my failure attempt:

这是我的失败尝试:

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())

This is the traceback:

这是追溯:

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 个解决方案

#1


0  

I just gave it a try and confirm that your code is fine as it works on my environment. Though, I run it on a Linux (Ubuntu 16.04) virtual machine. You could check what data you are getting back

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

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

or examine the content such as

或检查诸如的内容

print(res.text)

#1


0  

I just gave it a try and confirm that your code is fine as it works on my environment. Though, I run it on a Linux (Ubuntu 16.04) virtual machine. You could check what data you are getting back

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

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

or examine the content such as

或检查诸如的内容

print(res.text)