Newbie problem with urllib.request.urlopen
Bernie Connors
berniejconnors at gmail.com
Tue Sep 26 12:40:28 EDT 2017
On Tuesday, September 26, 2017 at 12:32:18 PM UTC-3, Bernie Connors wrote:
> Hello,
>
> My first post here on C.L.P. I have only written a few python scripts in 2.7 and now I'm trying my first python 3 script. Can you tell me why this snippet won't run?
> -----------------------
> from urllib.request import urlopen
>
> with urlopen('http://geonb.snb.ca/arcgis/rest/services/GeoNB_SNB_Parcels/MapServer/0/query?outSR=4617&f=JSON&where=PID='75385120'') as conn:
> print(conn)
> -----------------------
> Thanks,
> Bernie.
Thomas,
The PID parameter at the end of my url must be enclosed in single quotes, '75385120', or the API won't execute the query. I have the code in a python notebook on Azure -
https://notebooks.azure.com/n/n31C2DSCOr8/notebooks/URLopen%20Test.ipynb
Here are the error messages I am getting:
---------------------------------------------------------------------------
HTTPError Traceback (most recent call last)
<ipython-input-9-ec8ba282ee14> in <module>()
1 from urllib.request import urlopen
----> 2 with urlopen("http://geonb.snb.ca/arcgis/rest/services/GeoNB_SNB_Parcels/MapServer/0/query?outSR=4617&f=JSON&where=PID='75385120'") as conn:
3 print(conn)
~/anaconda3_410/lib/python3.5/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
160 else:
161 opener = _opener
--> 162 return opener.open(url, data, timeout)
163
164 def install_opener(opener):
~/anaconda3_410/lib/python3.5/urllib/request.py in open(self, fullurl, data, timeout)
469 for processor in self.process_response.get(protocol, []):
470 meth = getattr(processor, meth_name)
--> 471 response = meth(req, response)
472
473 return response
~/anaconda3_410/lib/python3.5/urllib/request.py in http_response(self, request, response)
579 if not (200 <= code < 300):
580 response = self.parent.error(
--> 581 'http', request, response, code, msg, hdrs)
582
583 return response
~/anaconda3_410/lib/python3.5/urllib/request.py in error(self, proto, *args)
507 if http_err:
508 args = (dict, 'default', 'http_error_default') + orig_args
--> 509 return self._call_chain(*args)
510
511 # XXX probably also want an abstract factory that knows when it makes
~/anaconda3_410/lib/python3.5/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
441 for handler in handlers:
442 func = getattr(handler, meth_name)
--> 443 result = func(*args)
444 if result is not None:
445 return result
~/anaconda3_410/lib/python3.5/urllib/request.py in http_error_default(self, req, fp, code, msg, hdrs)
587 class HTTPDefaultErrorHandler(BaseHandler):
588 def http_error_default(self, req, fp, code, msg, hdrs):
--> 589 raise HTTPError(req.full_url, code, msg, hdrs, fp)
590
591 class HTTPRedirectHandler(BaseHandler):
HTTPError: HTTP Error 403: Forbidden
--------------------------------------
Thanks,
Bernie.
More information about the Python-list
mailing list