Python read json from url
To me below code is working- in Python 2.7
import jsonfrom urllib import urlopenurl_address = ['https://api.twitter.com/1.1/statuses/user_timeline.json']for i in url_address: resp = urlopen(i) data = json.loads(resp.read())print(data)
Output-
{u'errors': [{u'message': u'Bad Authentication data.', u'code': 215}]}
If you use requests
module-
import requestsurl_address = ['https://api.twitter.com/1.1/statuses/user_timeline.json']for i in url_address: resp = requests.get(i).json() print resp
Output-
{u'errors': [{u'message': u'Bad Authentication data.', u'code': 215}]}
twitter is returning 400 code(bad request) so exception is firing.you can use try...except in your code sample and read exception body to get response json {"errors":[{"code":215,"message":"Bad Authentication data."}]}
import jsonimport urllib.requestfrom urllib.error import HTTPErrortry: url_address ='https://api.twitter.com/1.1/statuses/user_timeline.json' with urllib.request.urlopen(url_address) as url: data = json.loads(url.read()) print(data)except HTTPError as ex: print(ex.read())
I prefer aiohttp
, it's asynchronous. Last time I checked, urllib
was blocking. This means that it will prevent other parts of the script from running while it itself is running. Here's an example for aiohttp
:
import aiohttpasync with aiohttp.ClientSession() as session:async with session.get('https://api.github.com/users/mralexgray/repos') as resp: print(await resp.json())