0
對於一個項目,我試圖通過NS api對NS(Nederlandse Spoorwegen)數據進行一些分析。python:acces NS api
奇怪的是,從http://webservices.ns.nl/ns-api-avt?station=Dordrecht獲得站信息完美,但試圖從http://webservices.ns.nl/ns-api-stations獲得所有站導致HTTPError。
作品:
import urllib2
from xml.etree import ElementTree
import datetime, time, sys
theurl = 'http://webservices.ns.nl/ns-api-avt'
username = 'username'
password = 'password'
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, theurl, username, password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener)
specificURL = 'http://webservices.ns.nl/ns-api-avt?station=Dordrecht'
xmlHandle = urllib2.urlopen(specificURL)
不起作用:
theurl = 'http://webservices.ns.nl/ns-api-avt'
username = 'username'
password = 'password'
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, theurl, username, password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener)
specificURL = "http://webservices.ns.nl/ns-api-stations"
xmlHandle = urllib2.urlopen(specificURL)
這兩個程序是相同的,但產生不同的結果。我錯過了什麼嗎?
的錯誤代碼:
File "stations.py", line 32, in <module>
xmlHandle = urllib2.urlopen(specificURL)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 127, in urlopen
return _opener.open(url, data, timeout)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 410, in open
response = meth(req, response)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 523, in http_response
'http', request, response, code, msg, hdrs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 448, in error
return self._call_chain(*args)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 382, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 531, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 401: Unauthorized
如果您發佈錯誤的確切文本,它將會更容易幫助您! –
添加了錯誤。 – DCB