从指定URL中下载文件

 1 #!/usr/bin/env python3
 2 import requests
 3 
 4 def download(url):
 5     try:
 6         req = requests.get(url)
 7     except requests.exceptions.MissingSchema:
 8         print('Invalid URL "{}"'.format(url))
 9         return
10 
11     if req.status_code == 403:
12         print('You do not have the authority to access this page.')
13         return
14     filename = url.split('/')[-1]
15     with open(filename,'w') as fobj:
16         fobj.write(req.content.decode('utf-8'))
17     print("Download over.")
18 
19 if __name__ == '__main__':
20     url = input('Enter a URL: ')
21     download(url)

猜你喜欢

转载自www.cnblogs.com/--lr/p/11857996.html
今日推荐