Reputation: 33
after getting some help with my code, some errors still appear.
import urllib.request
import json
r = urllib.request.urlopen("http://www.countdown.tfl.gov.uk/stopBoard/50051").read()
rr = str(r)
obj = json.loads(rr)
# filter only the b16 objects
b16_objs = filter(lambda a: a['routeName'] == 'B16', obj['arrivals'])
if b16_objs:
# get the first item
b16 = b16_objs[0]
my_estimatedWait = b16['estimatedWait']
print(my_estimatedWait)
and this is the error that i get and im not sure how to fix this as im new to python and the raspberry pi 2. Thanks
File "/usr/lib/python3.2/json/decoder.py", line 369, in raw_decode
obj, end = self.scan_once(s, idx)
StopIteration
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "program6.py", line 6, in <module>
obj = json.loads(rr)
File "/usr/lib/python3.2/json/__init__.py", line 309, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.2/json/decoder.py", line 353, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.2/json/decoder.py", line 371, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
thanks for your help
Upvotes: 1
Views: 150
Reputation: 1207
I think luoluo answer should solve your problem. However I think you need to get a look on requests
library since it has built-in json decoder
Your code using requests:
import requests
obj = requests.get("http://www.countdown.tfl.gov.uk/stopBoard/50051").json()
b16_objs = list(filter(lambda a: a['routeName'] == 'B16', obj['arrivals']))
if b16_objs:
estimated_wait = b16_objs[0]['estimatedWait']
print(estimated_wait)
Upvotes: 0
Reputation: 5533
You need to decode the bytes object to produce a string
This would solve your problem.
>>> import urllib.request
>>> import json
>>> r = urllib.request.urlopen("http://www.countdown.tfl.gov.uk/stopBoard/50051").read()
>>> json.loads(r.decode('utf-8'))
{'arrivals': [{'isCancelled': False, 'scheduledTime': '08:37', 'isRealTime': True, 'estimatedWait': 'due', 'destination': 'Erith', 'routeName': 'B12', 'routeId': 'B12'}, {'isCancelled': False, 'scheduledTime': '08:37', 'isRealTime': True, 'estimatedWait': 'due', 'destination': 'Horn Park', 'routeName': 'B15', 'routeId': 'B15'}, {'isCancelled': False, 'scheduledTime': '08:37', 'isRealTime': True, 'estimatedWait': '1 min', 'destination': 'Lewisham Centre', 'routeName': '89', 'routeId': '89'}, {'isCancelled': False, 'scheduledTime': '08:37', 'isRealTime': True, 'estimatedWait': '1 min', 'destination': 'Woolwich', 'routeName': '96', 'routeId': '96'}, {'isCancelled': False, 'scheduledTime': '08:39', 'isRealTime': True, 'estimatedWait': '2 min', 'destination': 'New Eltham', 'routeName': 'B13', 'routeId': 'B13'}, {'isCancelled': False, 'scheduledTime': '08:45', 'isRealTime': True, 'estimatedWait': '8 min', 'destination': 'Kidbrooke', 'routeName': 'B16', 'routeId': 'B16'}, {'isCancelled': False, 'scheduledTime': '08:48', 'isRealTime': True, 'estimatedWait': '11 min', 'destination': 'North Greenwich', 'routeName': '486', 'routeId': '486'}, {'isCancelled': False, 'scheduledTime': '08:51', 'isRealTime': True, 'estimatedWait': '14 min', 'destination': 'Woolwich', 'routeName': '96', 'routeId': '96'}, {'isCancelled': False, 'scheduledTime': '08:55', 'isRealTime': True, 'estimatedWait': '19 min', 'destination': 'Horn Park', 'routeName': 'B15', 'routeId': 'B15'}, {'isCancelled': False, 'scheduledTime': '08:57', 'isRealTime': True, 'estimatedWait': '20 min', 'destination': 'Lewisham Centre', 'routeName': '89', 'routeId': '89'}, {'isCancelled': False, 'scheduledTime': '08:58', 'isRealTime': True, 'estimatedWait': '22 min', 'destination': 'North Greenwich', 'routeName': '422', 'routeId': '422'}], 'lastUpdated': '09:36', 'filterOut': [], 'serviceDisruptions': {'infoMessages': [], 'criticalMessages': [], 'importantMessages': []}}
Upvotes: 0