-1

I am trying to download a zip file from a secure website, this is my code:

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")

The errors include: syntax error, invalid syntax. Can someone help me out please. Thanks

Estefy
  • 424
  • 2
  • 6
  • 20

1 Answers1

1

In order to access the contents of /files2.php you must login using the form in /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))

Then you can use opener to download the files and write to disk, example:

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())
t.m.adam
  • 15,106
  • 3
  • 32
  • 52