【Python】如何下载单张网络图片到本地

【需求】

已知网络图片地址,如果使用python程序将其下载到本地?

【代码】

#encoding=utf-8
#引入所用的包
import urllib.request # 已知网络图片文件的地址 url="http://pic.09nh.com/p1682/2021/2/21/p1682-0647-89706.jpg"

# 打开此网络文件,响应都在response里 with urllib.request.urlopen(url) as response: # 远端的二进制数据
data
=response.read()
# 本地文件名 filename
="1.jpg" # 写二进制文件 with open(filename,'wb') as f: f.write(data)

# 写完了报知一声
print("download successfully!")

END

posted @ 2020-09-20 08:37  不朽的飞翔  阅读(127)  评论(0编辑  收藏  举报