ValueError:没有使用Python解码JSON对象

时间:2016-05-27 06:06:40

标签: python json

我有一个有效的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,而在浏览器中打开的链接是JSON(Chrome)

1 个答案:

答案 0 :(得分:0)

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

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'>

你走了!

编辑:死亡追踪者是对的,你在网站上打了太多次,这就是为什么你没有得到你要求的页面,但代码很好,我展示的是简化它的方法。除非您使用其他IP地址查询网站,否则我无法找到解决方案。