Python Small Script Program

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.