简体   繁体   English

从URLOpen获取JSON

[英]Getting JSON From URLOpen

I cannot consistently get JSON from a given url. 我无法始终从给定的URL获取JSON。 It works only about 60% of the time 只有大约60%的时间有效

jsonurl = urlopen('http://www.reddit.com/r/funny/hot.json?limit=16')
r_content = json.load(jsonurl)['data']['children']

The program crashes on the second line sometimes, because the info from the url is not retrieved properly for some reason 该程序有时会在第二行崩溃,因为由于某些原因无法正确检索网址中的信息

With some debugging, I found out that I was getting the following error from the first line: 通过一些调试,我发现从第一行得到以下错误:

<addinfourl at 4321460952 whose fp = <socket._fileobject object at 0x10185b050>>

This error occurs about 40% of the time, the other 60% of the time, the code works perfectly. 大约40%的时间会发生此错误,其他60%的时间会发生错误,代码可以正常工作。 What am I doing wrong? 我究竟做错了什么? How do I make the url opening more consistent? 如何使网址打开更加一致?

It is usually not an issue from the client side. 从客户端来看,这通常不是问题。 Your code is consistent in behavior but the server response can vary. 您的代码行为一致,但是服务器响应可能会有所不同。

I ran your code a few times and It does throw up certain issues: 我运行了几次您的代码,它确实引发了某些问题:

>>> jsonurl = urlopen('http://www.reddit.com/r/funny/hot.json?limit=16')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 406, in open
    response = meth(req, response)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 519, in http_response
    'http', request, response, code, msg, hdrs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 444, in error
    return self._call_chain(*args)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 378, in _call_chain
    result = func(*args)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 527, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 429: Unknown

You have to handle cases where server response is anything but HTTP 200. You can wrap your code in a try / except block and you should pass jsonurl to json.loads() only when your request succeeds. 您必须处理服务器响应不是HTTP 200的情况。您可以将代码包装在try / except块中,并且仅当请求成功时才应将jsonurl传递给json.loads()。

Also urlopen returns a file-like descriptor. urlopen还返回类似文件的描述符。 Hence if you print jsourl , it simply provides jsonurl.__repr__() value. 因此,如果您print jsourl ,它仅提供jsonurl.__repr__()值。 See below: 见下文:

>>> jsonurl.__repr__()
'<addinfourl at 4393153672 whose fp = <socket._fileobject object at 0x105978450>>'

You have to look for the following:: 您必须寻找以下内容:

>>> jsonurl.getcode()
200
>>> 

and only if it 200, should you process the data obtained from the request. 并且仅当它为200时,才应处理从请求中获得的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM