2014-04-03 5 views
1

json 코드가 정확하더라도이 오류가 발생했습니다 (JSON Validator으로 테스트 함). 여기ValueError : 유효한 json으로 JSON 객체를 디코딩 할 수 없습니다.

import urllib.request, urllib.parse 
import json 

num_queries = 50*4 
query = urllib.parse.urlencode({'q' : 'example'}) 
url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' % query 

for start in range(0, num_queries, 4): 
    request_url = '{0}&start={1}'.format(url, start) 
    print(request_url) 
    request = urllib.request.Request(request_url) 
    request.add_header('User-Agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)') 
    search_results = urllib.request.urlopen(request) 
    encoding = search_results.headers.get_content_charset() 
    print(search_results.read().decode(encoding)) 
    json = json.loads(search_results.read().decode(encoding)) 
    results = json['responseData']['results'] 
    for i in results: 
     print (i['url']) 

를 응답 내가 얻을입니다 : 여기 내 코드는

{ 
"responseData": { 
    "results": [ 
     { 
      "GsearchResultClass": "GwebSearch", 
      "unescapedUrl": "http://example.com/", 
      "url": "http://example.com/", 
      "visibleUrl": "example.com", 
      "cacheUrl": "http://www.google.com/search?q=cache:OEM9MzUDrRQJ:example.com", 
      "title": "<b>Example</b> Domain", 
      "titleNoFormatting": "Example Domain", 
      "content": "<b>Example</b> Domain. This domain is established to be used for illustrative <b>examples</b> \nin documents. You may use this domain in <b>examples</b> without prior coordination ..." 
     }, 
     { 
      "GsearchResultClass": "GwebSearch", 
      "unescapedUrl": "http://fr.wikipedia.org/wiki/Example_(chanteur)", 
      "url": "http://fr.wikipedia.org/wiki/Example_(chanteur)", 
      "visibleUrl": "fr.wikipedia.org", 
      "cacheUrl": "http://www.google.com/search?q=cache:KQgswW_0sBoJ:fr.wikipedia.org", 
      "title": "<b>Example</b> (chanteur) — Wikipédia", 
      "titleNoFormatting": "Example (chanteur) — Wikipédia", 
      "content": "Elliot John Gleave plus connu sous le pseudonyme <b>Example</b> (né le 20 juin 1982) \nest un chanteur et rappeur britannique. Son nom de scène vient de ses ..." 
     }, 
     { 
      "GsearchResultClass": "GwebSearch", 
      "unescapedUrl": "http://www.trythisforexample.com/", 
      "url": "http://www.trythisforexample.com/", 
      "visibleUrl": "www.trythisforexample.com", 
      "cacheUrl": "http://www.google.com/search?q=cache:VErXNqtEJDsJ:www.trythisforexample.com", 
      "title": "<b>Example</b>", 
      "titleNoFormatting": "Example", 
      "content": "Official site for <b>Example</b>. ... Pre-order New Album · Tour Dates · Merch · Twitter · \nInstagram · Facebook · Youtube <b>Example</b> © 2013 Epic Records Privacy Policy." 
     }, 
     { 
      "GsearchResultClass": "GwebSearch", 
      "unescapedUrl": "http://www.youtube.com/watch?v=CLXt3yh2g0s", 
      "url": "http://www.youtube.com/watch%3Fv%3DCLXt3yh2g0s", 
      "visibleUrl": "www.youtube.com", 
      "cacheUrl": "http://www.google.com/search?q=cache:9CKsisLTCjgJ:www.youtube.com", 
      "title": "<b>Example</b> - &#39;Changed The Way You Kiss Me&#39; (Official Video) - YouTube", 
      "titleNoFormatting": "Example - &#39;Changed The Way You Kiss Me&#39; (Official Video) - YouTube", 
      "content": "Apr 21, 2011 <b>...</b> Download <b>Example&#39;s</b> new single &#39;Perfect Replacement&#39; on iTunes (Out Now): http\n://www.smarturl.it/PERFECTREPLACEMENT For more Click ..." 
     } 
    ], 
    "cursor": { 
     "resultCount": "157 000 000", 
     "pages": [ 
      { 
       "start": "0", 
       "label": 1 
      }, 
      { 
       "start": "4", 
       "label": 2 
      }, 
      { 
       "start": "8", 
       "label": 3 
      }, 
      { 
       "start": "12", 
       "label": 4 
      }, 
      { 
       "start": "16", 
       "label": 5 
      }, 
      { 
       "start": "20", 
       "label": 6 
      }, 
      { 
       "start": "24", 
       "label": 7 
      }, 
      { 
       "start": "28", 
       "label": 8 
      } 
     ], 
     "estimatedResultCount": "157000000", 
     "currentPageIndex": 0, 
     "moreResultsUrl": "http://www.google.com/search?oe=utf8&ie=utf8&source=uds&start=0&hl=fr&q=example", 
     "searchResultTime": "0,22" 
    } 
}, 
"responseDetails": null, 
"responseStatus": 200 
} 

오류 :

Traceback (most recent call last): 
    File "test.py", line 16, in <module> 
    text = json.loads(search_results.read().decode(encoding)) 
    File "/usr/lib/python3.3/json/__init__.py", line 319, in loads 
    return _default_decoder.decode(s) 
    File "/usr/lib/python3.3/json/decoder.py", line 352, in decode 
    obj, end = self.raw_decode(s, idx=_w(s, 0).end()) 
    File "/usr/lib/python3.3/json/decoder.py", line 370, in raw_decode 
    raise ValueError("No JSON object could be decoded") 
ValueError: No JSON object could be decoded 
+0

어디서 오류가 있습니까? – SzieberthAdam

답변

2

원시 json 문자열을 인쇄 할 때 search_results.read()을 호출하면 요청 버퍼가 비게됩니다.

JSON을 디코딩하기 위해 다시 호출하면 버퍼가 비어 있으며 반환하는 빈 문자열을 디코딩하지 못합니다.

0

귀하의 JSON 그러나 그것의 일부 문자열은 줄 바꿈 문자 \n를 포함, 유효합니다. 문자열의 일부인 것을 의미하는 경우 이스케이프해야합니다 (전에 \ 추가). 문자열의 일부가 아닌 경우에는 문자열을 제거해야합니다.

기본적으로 \\n으로 JSON의 \n을 모두 바꾸거나 제거해야합니다.

비슷한 문제가 here으로 대답되었습니다.

+1

이것은 json이 Python 문자열 _literal_에있는 경우에만 관련이 있습니다. 이 경우, op는 서버로부터 그것을 얻습니다. – Eric

+0

좋은 지적, 나는 잘못 읽었다. –

관련 문제