一:使用Python中的urllib类中的urlretrieve()函数,直接从网上下载资源到本地,具体代码:
import os,stat import urllib.request img_url="https://timgsa.baidu.com/timg" "d44&imgtype=jpg&er=1&src=http%3A%2F%2Fupload.qianhuaweb.com%2F2017%2F0718%2F1500369506683.jpg" file_path='D:/book/img' file_name ="pyt" try: #是否有这个路径 if not os.path.exists(file_path): #创建路径 os.makedirs(file_path) #获得图片后缀 file_suffix = os.path.splitext(img_url)[1] print(file_suffix) #拼接图片名(包含路径) filename = '{}{}{}{}'.format(file_path,os.sep,file_name,file_suffix) print(filename) #下载图片,并保存到文件夹中 urllib.request.urlretrieve(img_url,filename=filename) except IOError as e: print("IOError") except Exception as e: print("Exception")
二:利用读写操作写入文件,具体代码:
import os,stat import urllib.request for i in range(1,3): if not os.path.exists("./rym"): print("不纯在") os.makedirs("./rym") else: print("存在") os.chmod("D:/imagss",777) with urllib.request.urlopen("https://timgsa.baidu.com/timg" "b301fea27c2149a7070d44&imgtype=jpg&er=1&src=http%3A%2F%2Fupload.qianhuaweb.com%2F2017%2F0718%" "2F1500369506683.jpg", timeout=30) as response, open("./rym/lyj.png" , 'wb') as f_save: f_save.write(response.read()) f_save.flush() f_save.close() print("成功")
以上所述是小编给大家介绍的Python下载图片并保存本地的两种方式详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
风云阁资源网 Design By www.bgabc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
风云阁资源网 Design By www.bgabc.com
暂无评论...
更新日志
2024年09月20日
2024年09月20日
- 战锤40K星际战士2狙击职业介绍|狙击职业技能效果一览
- RobertSaxtonTheResurrectionoftheSoldiers(2024)[24Bit-192kHz]FLAC
- 唐成杨东哥弹唱《情若真HQ》头版限量编号[WAV+CUE]
- 柏菲·HIFI风情1《一杯香茶》限量开盘母带ORMCD[WAV+CUE]
- 发糖了!《黑神话》动画导演绘制八戒紫蛛儿中秋贺图
- “时光系列”新作《时光3:永耀之境》首曝:画风更真实的恋爱模拟RPG
- 《战地》游戏主管:始终认为《战地》未来销量能超越《使命召唤》
- 《伤感民谣 值得收藏的音乐极品 2CD》[WAV/分轨][1.2GB]
- 《邓紫棋 巨肺唱将小天后有心人 2CD》[WAV/分轨][1.2GB]
- 《迪克牛仔 坛奇精选 2CD台首版》 [WAV+CUE][600MB]
- 陈明.1998-为了爱影视歌曲+新歌【中唱】【WAV+CUE】
- 黄小琥.2009-简单/不简单【华纳】【WAV+CUE】
- 洪卓立.2010-TASTE.OF.LOVE新曲+精丫英皇娱乐】【WAV+CUE】
- 战锤40K星际战士2职业强度排行
- 交错战线PVE国家队成员以及常用角色推荐