Python下载文件不工作

2024-04-18 11:16:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试下载文件/阅读页面内容。url通过Siteminder身份验证进行身份验证。我正在使用下面的代码,但是

401: Authentication Error

import urllib
import urllib.parse
import urllib.request
import ssl

context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
context.verify_mode = ssl.CERT_REQUIRED
context.check_hostname = True
context.load_default_certs()

password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
url = "https://myurl.com/blah..."
password_mgr.add_password(None, url, 'myuserID', 'myPassword')
handler = urllib.request.HTTPBasicAuthHandler(password_mgr)
opener = urllib.request.build_opener(handler)
urllib.request.install_opener(opener)
req = urllib.request.urlopen(url, context=context).read()
print(response.text)

我也尝试了下面的requests,但我得到了相同的错误。在

^{pr2}$

我使用的是windows10和python3.5。在

但如果我在UNIX系统中运行它,那么它就可以工作了,我可以下载文件:

wget --secure-protocol=TLSv1 --no-check-certificate --user=myuserID --password=myPassword 'https://myurl.com/blah...'

python代码有什么问题?在

更新: 如果我使用cookies使用下面的代码,那么会下载1个html文件:当我在IE中打开html文件时,它再次请求凭据,然后下载确切的文件。在

import urllib
import urllib.parse
import urllib.request
import http.cookiejar

url = "https://myurl.com/blah..."
username = 'myuserID'
password = 'myPassword'

cj = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
opener.addheaders = [('User-agent', 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)')]
params = urllib.parse.urlencode({'user': username, 'password': password}).encode("utf-8")
print(params)
opener = urllib.request.build_opener(
    urllib.request.HTTPRedirectHandler(),
    urllib.request.HTTPHandler(debuglevel=0),
    urllib.request.HTTPSHandler(debuglevel=0),
    urllib.request.HTTPCookieProcessor(cj))

urllib.request.install_opener(opener)
page = urllib.request.urlopen(url, params).read()
print(page)

html如下:

<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY onLoad="document.AUTOSUBMIT.submit();">
This page is used to hold your data while you are being authorized for your request.<BR><BR>You will be forwarded to continue the authorization process. If this does not happen automatically, please click the Continue button below.
<FORM NAME="AUTOSUBMIT" METHOD="POST" ENCTYPE="application/x-www-form-urlencoded" ACTION="https://myurl.com/blah...">
<INPUT TYPE="HIDDEN" NAME="SMPostPreserve" VALUE="gPcBiwAFuJyNjkpY2Lq/i3Iq80qHAGVxfmNyp3VohPbmmfdNGf5bEhuAZXmqUWJ+ftkJ4uZvFjrnDf+sGAm13m5CDGTljCCF">
<INPUT TYPE="SUBMIT" VALUE="Continue">
</FORM>
</BODY>
</HTML>

Tags: 文件代码httpsimportcomurlsslparse