Skip to content

Commit 7ccdf07

Browse files
author
tuntun
committed
使用requests保存远程图片(或文件)
1 parent 1d863b9 commit 7ccdf07

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

save_remote_image.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# 使用requests保存远程图片(或文件)
4+
import requests
5+
import os
6+
7+
8+
def saveRemoteImage():
9+
imgurl = 'http://www.tantengvip.com/wp-content/uploads/2015/01/cropped-13887362_13887362_1347773771848.jpg'
10+
filename = imgurl.split('/')[-1]
11+
path = './static/'+filename
12+
if not os.path.exists(path):
13+
r = requests.get(imgurl)
14+
with open(path, 'wb') as f:
15+
f.write(r.content)
16+
print('OK')
17+
else:
18+
print('Already exists.')
19+
20+
"""
21+
下载大文件这样写:
22+
for chunk in r.iter_content():
23+
f.write(chunk)
24+
"""
25+
26+
saveRemoteImage()
112 KB
Loading

0 commit comments

Comments
 (0)