我有一個有效的JSON,我是無法使用Python和收到錯誤讀取解碼,ValueError異常:無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<=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
進行驗證。這裏有什麼問題?
當我使用打印response.text
,我得到下面的輸出:
u'<html><head><title>Zillow: Real Estate, Apartments, Mortgage & 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 )
你使用的是什麼版本的Python?代碼似乎運行良好,我複製粘貼你的代碼片段,並得到一個有效的JSON當我打印(json_response) – glls
'Python 2.7.9'是版本 – Arefe
該代碼在v2.7爲我工作。這可能是您在獲取響應文本時出錯。嘗試'print(response.text)'並檢查是否有來自服務器的json響應。 – Ananth