[問題] HTTP登入

作者: robert780612 (.....)   2014-07-10 21:04:54
我想從 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認證的部份,我完全沒有概念,所以我想請問要怎麼解決這個問題?
還有相關的知識要去哪裡才能找到,我完全不知從何下手
帳號、密碼我確定是對的,從瀏覽器上都可以下載
先謝謝大家了。
作者: kilfu0701 (享受人生快樂至上)   2014-07-10 23:47:00
http://pastie.org/9374725 需要設realm吧
作者: robert780612 (.....)   2014-07-11 05:02:00
我用你的case1成功了,太感謝了

Links booklink

Contact Us: admin [ a t ] ucptt.com