【发布时间】:2018-07-06 07:37:17
【问题描述】:
我在 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)
【问题讨论】:
-
好吧,如果响应中包含非 JSON 内容,则会导致问题
-
对于那个 url 我得到
416 Requested Range Not Satisfiable - The file is already fully retrieved; nothing to do这解释了为什么JSONDecoder抱怨 json 是空的(第 1 行第 1 列的错误通常是空数据)。 -
这个网址有什么我可以做的吗?我的意思是,有什么可以获取数据的吗?
-
它看起来可能是一个“一次性”网址 - 一旦您获得了数据,它就不再适用于未来的请求了。
-
使用您发布的代码多次为我获取该数据没问题。
res.status_code的值是多少?
标签: python json python-3.x web-scraping