python:access NS api
对于一个附带项目,我试图通过NS api对NS(Nederlandse Spoorwegen)数据进行一些分析 奇怪的是,从中获取站点信息效果很好,但是尝试从中获取所有站点会导致HTTPError 有效:python:access NS api,python,api,Python,Api,对于一个附带项目,我试图通过NS api对NS(Nederlandse Spoorwegen)数据进行一些分析 奇怪的是,从中获取站点信息效果很好,但是尝试从中获取所有站点会导致HTTPError 有效: import urllib2 from xml.etree import ElementTree import datetime, time, sys theurl = 'http://webservices.ns.nl/ns-api-avt' username = 'username' p
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
不起作用:
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
这两个例程是相同的,但产生不同的结果。我错过什么了吗
错误代码:
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
文件“stations.py”,第32行,在
xmlHandle=urllib2.urlopen(specificURL)
urlopen中的文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py”,第127行
return\u opener.open(url、数据、超时)
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py”,第410行,打开
响应=方法(请求,响应)
http_响应中的文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py”,第523行
“http”、请求、响应、代码、消息、hdrs)
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py”,第448行出错
返回自我。调用链(*args)
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py”,第382行,在调用链中
结果=func(*args)
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py”,第531行,http\u error\u default
raise HTTPError(请求获取完整url(),代码,消息,hdrs,fp)
urllib2.HTTPError:HTTP错误401:未经授权
(部分)已解决
一个简单的代码,如下所示:
import requests
url = 'http://webservices.ns.nl/ns-api-stations-v2'
username = 'username'
password = 'password'
print(requests.get(url, auth=(username, password)).content)
工作!对于这两个URL。奇怪的是,另一种方法不适用于两种情况,而只适用于一种情况。如果您发布错误的确切文本,将更容易帮助您!