【问题标题】:ValueError: No JSON object could be decoded using PythonValueError:无法使用 Python 解码 JSON 对象
【发布时间】:2016-05-27 15:30:51
【问题描述】:

我有一个有效的 json,我无法使用 Python 读取并收到错误,

ValueError: No JSON object could be decoded using Python 

代码如下,

import json, requests

page = "http://www.zillow.com/search/GetResults.htm?spt=homes&status=110001&lt=001000&ht=111111&pr=,&mp=,&bd=2%2C&ba=0%2C&sf=,&lot=,&yr=,&pho=0&pets=0&parking=0&laundry=0&income-restricted=0&pnd=0&red=0&zso=0&days=any&ds=all&pmf=1&pf=1&zoom=3&rect=-134340820,16594081,-56469727,54952386&p=1&sort=globalrelevanceex&search=maplist&disp=1&listright=true&isMapSearch=true&zoom=3"
response = requests.get(page) # request the json file
json_response =  json.loads(response.text) # parse the json file

当我在浏览器中打开 URL 时,我能够正确地看到 JSON 文件并且可以使用网站进行验证:http://codebeautify.org/jsonviewer。这里有什么问题?

当我使用 print response.text 时,我得到以下输出:

u'<html><head><title>Zillow: Real Estate, Apartments, Mortgage &amp; Home Values in the US</title><meta http-equiv="X-UA-Compatible" content="IE=8, IE=9"/><meta name="ROBOTS" content="NOINDEX, NOFOLLOW"/><link href="//fonts.googleapis.com/css?family=Open+Sans:400&subset=latin" rel="stylesheet" type="text/css"/><link href="http://www.zillowstatic.com/vstatic/9520695/static/css/z-pages/captcha.css" type="text/css" rel="stylesheet" media="screen"/><script language="javascript">\n            function onReCaptchaLoad() {\n                window.reCaptchaLoaded = true;\n            }\n\n            window.setTimeout(function () {\n                if (!window.reCaptchaLoaded) {\n                   document.getElementById(\'norecaptcha\').value = true;\n                   document.getElementById(\'captcha-form\').submit();\n                }\n            }, 5000);\n        </script></head><body><main class="zsg-layout-content"><div class="error-content-block"><div class="error-text-content"><!-- <h1>Captcha</h1> --><h5>Enter the characters in the images to continue.</h5><div id="content" class="captcha-container"><form method="POST" action="" id="captcha-form"><script type="text/javascript">\r\nvar RecaptchaOptions = {"theme":"white","lang":"en-US"};\r\n</script>\r\n<script type="text/javascript" src="http://api.recaptcha.net/challenge?k=6Lf2nvMSAAAAAMQ5p6WlAfDEixMdOQgJsij-3_ud" onload="onReCaptchaLoad()"></script>\r\n<br/><input id="dest" name="dest" type="hidden" value="ognl:originalDestination"/><input id="norecaptcha" name="norecaptcha" type="hidden" value="false"/><button type="submit" class="zsg-button zsg-button_primary">Submit</button></form><img src="http://www.zillowstatic.com/static/logos/logo-65x14.png" width="65" alt="Zillow" height="14"></img></div></div></div></main></body></html><!-- H:049  T:0ms  S:1494  R:Thu May 26 23:12:41 PDT 2016  B:5.0.29554-release_20160512-lunar_lander.6d4c099~candidate.d23c8e0 -->'

所以,我似乎没有从服务器获取 JSOn,而在浏览器 (Chrome) 中打开的链接是 JSON

【问题讨论】:

  • 你用的是什么版本的python?代码似乎运行良好,我复制粘贴了你的 sn-p 并在我打印时得到一个有效的 json(json_response)
  • Python 2.7.9 是版本
  • 该代码在 v2.7 上为我工作。可能是您在获取响应文本时出错。尝试print(response.text) 并检查您是否有来自服务器的 json 响应。
  • 所以,我没有从服务器获取 JSON,只有 HTML
  • 您似乎已经很难访问这个网站了。上面的html是一个RECAPTACHA

标签: python json


【解决方案1】:

如果您将请求用作库,则可以执行以下操作:

import requests as req

page = "http://www.zillow.com/search/GetResults.htm?spt=homes&status=110001&lt=001000&ht=111111&pr=,&mp=,&bd=2%2C&ba=0%2C&sf=,&lot=,&yr=,&pho=0&pets=0&parking=0&laundry=0&income-restricted=0&pnd=0&red=0&zso=0&days=any&ds=all&pmf=1&pf=1&zoom=3&rect=-134340820,16594081,-56469727,54952386&p=1&sort=globalrelevanceex&search=maplist&disp=1&listright=true&isMapSearch=true&zoom=3"

json_response = req.get(page).json()

print type(json_response)

>> <type 'dict'>

给你!

编辑:Death-Stalker 是对的,您访问该网站的次数太多,这就是您没有获得您请求的页面的原因,但是代码很好,我展示的是简化它的方法。除非您使用其他 IP 地址来查询网站,否则我看不到解决方案。

【讨论】:

  • 和以前一样ValueError: No JSON object could be decoded我相信我在网站上工作了很长时间,网站正在跟踪它。
猜你喜欢
  • 2023-03-12
  • 2015-01-04
  • 2017-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-29
  • 1970-01-01
相关资源
最近更新 更多