00、下載檔案

三角形發表於2019-04-11
 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)

 

相關文章