[Tutor] Retrieving data from a web site

Phil phil_lor at bigpond.com
Mon May 20 04:19:08 CEST 2013


On 19/05/13 18:05, Peter Otten wrote:
>
> The original Python 2 code:
>
>   $ cat parse.py
> import urllib2
> import json
>
> url = "http://*********/goldencasket"
> s = urllib2.urlopen(url).read()
>
> s = s.partition("latestResults_productResults")[2].lstrip(" =")
> s = s.partition(";")[0]
> data = json.loads(s)
> lotto = data["GoldLottoSaturday"]
> print lotto["drawDayDateNumber"]
> print map(int, lotto["primaryNumbers"])
> print map(int, lotto["secondaryNumbers"])
> $ python parse.py
> Sat 18/May/13, Draw 3321
> [14, 31, 16, 25, 6, 3]
> [9, 35]
>

It turns out that urllib2 and 3 are both built into python so I didn't 
have to stress over the dependency error. However, I do have an error 
and I'm not completely certain that I understand how the code provided 
by Peter works. The following is the error message:

Traceback (most recent call last):
   File "/home/phil/Python/lotto.py", line 10, in <module>
     data = json.loads(s)
   File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
     return _default_decoder.decode(s)
   File "/usr/lib/python2.7/json/decoder.py", line 365, in decode
     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
   File "/usr/lib/python2.7/json/decoder.py", line 383, in raw_decode
     raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

-- 
Regards,
Phil


More information about the Tutor mailing list