Simple code example
<span style= "FONT-SIZE:18PX;" >import urllib2 from base64 import encodestring url = ' http://www.xxx.com/xxxx.csv ' user = ' aaa ' passwd = ' bbbbb ' req = Urllib2. Request (URL) basestr = encodestring ('%s:%s '% (USER,PASSWD)) [: -1] Req.add_header (' Authorization ', ' Basic%s '% basestr) f = Urllib2.urlopen (req) data = F.read () with open ("2015010100.csv", "W") as Code: code.write (data) </span>
Description:
<span style= "FONT-SIZE:18PX;" >url = ' Http://www.xxx.com/xxxx.csv ' </span>
is the address of the file you want to download, if it is accessed in the browser, it will pop up a box to let you enter the account number, password.
The difference between having a basic certification is this sentence.
<span style= "FONT-SIZE:18PX;" >req.add_header (' Authorization ', ' Basic%s '% basestr) </span>
You need to add a user name and password to the request.
Finally, you can access it with Urllib2.open ().
Then open the file and save it.
Python HTTP Basic authentication + download file to local