00、下载文件

 1 import requests
 2 
 3 
 4 # 1、下载文本文件,并已 utf-8 编码保存
 5 
 6 res = requests.get('https://localprod.pandateacher.com/python-manuscript/crawler-html/exercise/HTTP%E5%93%8D%E5%BA%94%E7%8A%B6%E6%80%81%E7%A0%81.md')
 7 res.encoding = 'utf-8' # 定义response对象的编码类型,否则容易出现乱码
 8 with open('test.txt','w',encoding='utf-8') as download: # 定义写文件时的编码类型,和上一行是两回事,但是要与上一行定义的编码类型一致
 9     download.write(res.text)
10 
11 
12 # 2、下载二进制文件 mp3
13 
14 res = requests.get('https://static.pandateacher.com/Over%20The%20Rainbow.mp3')
15 with open('test.mp3','wb') as download:
16     download.write(res.content)
17 
18 
19 # 3、下载二进制文件 png
20 
21 res = requests.get('https://res.pandateacher.com/2019-01-12-15-29-33.png')
22 with open('test.png','wb') as download:
23     download.write(res.content)

 

posted @ 2019-04-11 20:31  三角形  阅读(331)  评论(0编辑  收藏  举报