1
我一直在閱讀關於Python的urllib2打開和讀取受密碼保護的目錄的能力,但即使在查看文檔中的示例以及StackOverflow中的示例後,我也無法獲取腳本上班。Python auth_handler不爲我工作
import urllib2
# Create an OpenerDirector with support for Basic HTTP Authentication...
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(realm=None,
uri='https://webfiles.duke.edu/',
user='someUserName',
passwd='thisIsntMyRealPassword')
opener = urllib2.build_opener(auth_handler)
# ...and install it globally so it can be used with urlopen.
urllib2.install_opener(opener)
socks = urllib2.urlopen('https://webfiles.duke.edu/?path=/afs/acpub/users/a')
print socks.read()
socks.close()
當我打印內容時,它會打印登錄屏幕的內容,我嘗試打開的網址會將您重定向到。任何人都知道這是爲什麼?