我正在尝试从安全网站下载zip文件,这是我的代码:
import urllib
username = "us"
password = "pass"
url = "https://ssl.isr.umich.edu/hrs/files2.php?versid=189"
testfile = urllib.FancyURLopener(username,password)
testfile.retrieve(url,"filetest1.pdf")
错误包括:语法错误,语法无效。有人可以帮帮我吗。感谢
答案 0 :(得分:1)
要访问/files2.php
的内容,您必须使用/login2.php
import urllib2
from urllib import urlencode
from cookielib import CookieJar
username = "us"
password = "pass"
login = 'https://ssl.isr.umich.edu/hrs/login2.php'
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(CookieJar()))
data = {'fuser':username, 'fpass':password, 'submit':'Login'}
opener.open(login, data=urlencode(data))
然后您可以使用opener
下载文件并写入磁盘,例如:
url = 'https://ssl.isr.umich.edu/hrs/filedownload2.php?d=833'
r = opener.open(url)
with open("filetest1.pdf", "wb") as f:
f.write(r.read())