00、下載文件
阿新 • • 發佈:2019-04-11
code imp sts https nco res with open 編碼 odin
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)
00、下載文件