a='http://wx1.sinaimg.cn/mw600/006HOayNgy1fqjdi2nxohj32pw3o8x6s.jpg' #圖片下載地址 ( 這里改成 文件txt地址)
w='/Users/kaibinliu/Desktop/rubbish/beautifulsoup4/123/123.jpg' #圖片存在的位置 (這里改成 文件txt的名字 也可以下載成功)
def download_file(url):
print('Downding %s' %url)
local_filename = url.split('/')[-1] #local_filename 就是下載鏈接最后的鏈接名稱
r = requests.get(url, stream=True) #請求下載的地址
with open(w, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024): #歷遍請求下載回來的圖片
if chunk: #如果chunk不等於0
f.write(chunk)
f.flush()# 刷新緩沖區
簡單的寫法 參考: http://www.runoob.com/python/file-flush.html 來的
其中with open(w, 'wb') as f: 可以 寫成 f=open(w, 'wb')
感覺應該也可以用下載文件的方法實現,有空試試 我還沒試
