The purpose of this article is to collect the simplest and most atomic blocks of code, which are simple, clear and easy to read and then set by the user. Please modify the Python code layout yourself.
Here is the index directory:
1. Download Web site files
2. Download multiple files on the website
1. Download Web site files
ImportUrllib2ImportOS//Forge Browser headers, mimic browser access//otherwise many sites will deny access to the 403 error that occurs headers= {'user-agent':'mozilla/5.0 (Windows; U Windows NT 6.1; En-us; rv:1.9.1.6) gecko/20091201 firefox/3.5.6'}
URL ='Https://abc.com/newnotes/a66.mp3'req= Urllib2. Request (URL, headers=headers)//If the file is large, please modify Timeoutf= Urllib2.urlopen (req,timeout=10) Data=F.read ()//locally saved file name local= Os.path.join ('/users/mymac/desktop/piano','A66.mp3') with open (local,"WB") as Code:code.write (data)
2. Download multiple files on the website
such as: Http://abc.com/a1.mp3 A2.mp3 A99.mp3
ImportUrllib2Importosheaders= {'user-agent':'mozilla/5.0 (Windows; U Windows NT 6.1; En-us; rv:1.9.1.6) gecko/20091201 firefox/3.5.6'} forIinchRange (1,100): Mp3name='b'+ STR (i) +'. mp3'URL='https://abc.com/newnotes/'+Mp3namePrintURL req= Urllib2. Request (URL, headers=headers)//Download Error skippedTry: F= Urllib2.urlopen (req,timeout=1000) exceptUrllib2. Urlerror, err:Print 'no file or download error...\n' ContinueData=f.read () local= Os.path.join ('/users/mymac/desktop/myfile', Mp3name) with open (local,"WB") as Code:code.write (data)
Python Small Script Program