我想从 http://tisv.freeway.gov.tw/ 下载高速公路车流的资料
下载资料的连结是 http://tisv.freeway.gov.tw/roadlevel_info.xml.gz
使用python 3.4,程式码如下
import urllib.request
url = 'http://tisv.freeway.gov.tw/roadlevel_info.xml.gz'
username = xxxxxxx
password = xxxxxxx
auth_handler = urllib.request.HTTPBasicAuthHandler()
auth_handler.add_password(None, url, username, password)
opener = urllib.request.build_opener(auth_handler)
urllib.request.install_opener(opener)
result = opener.open(url)
程式码是从 https://docs.python.org/3/library/urllib.request.html#examples
里的Use of Basic HTTP Authentication复制下来
只有在最后一行我把urlopen改成opener.open
但是跑了之后依然出现 HTTP Error 401
以下是error
Traceback (most recent call last):
File "<stdin>", line 26, in <module>
File "/usr/lib/python3.4/urllib/request.py", line 461, in open
response = meth(req, response)
File "/usr/lib/python3.4/urllib/request.py", line 571, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python3.4/urllib/request.py", line 499, in error
return self._call_chain(*args)
File "/usr/lib/python3.4/urllib/request.py", line 433, in _call_chain
result = func(*args)
File "/usr/lib/python3.4/urllib/request.py", line 579, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 401: Authorization Required
关于HTTP认证的部份,我完全没有概念,所以我想请问要怎么解决这个问题?
还有相关的知识要去哪里才能找到,我完全不知从何下手
帐号、密码我确定是对的,从浏览器上都可以下载
先谢谢大家了。