[問題] HTTP登入

看板Python作者 (.....)時間11年前 (2014/07/10 21:04), 編輯推噓1(101)
留言2則, 2人參與, 最新討論串1/1
我想從 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認證的部份,我完全沒有概念,所以我想請問要怎麼解決這個問題? 還有相關的知識要去哪裡才能找到,我完全不知從何下手 帳號、密碼我確定是對的,從瀏覽器上都可以下載 先謝謝大家了。 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.249.101.133 ※ 文章網址: http://www.ptt.cc/bbs/Python/M.1404997499.A.7EC.html

07/10 23:47, , 1F
http://pastie.org/9374725 需要設realm吧
07/10 23:47, 1F

07/11 05:02, , 2F
我用你的case1成功了,太感謝了
07/11 05:02, 2F
文章代碼(AID): #1JlezxVi (Python)
文章代碼(AID): #1JlezxVi (Python)