Python爬虫之Requests库——网络图片的爬取和存储

import requests
import os

url="http://image.nationalgeographic.com.cn/2017/0211/20170211061910157.jpg"
root="D://"
path=root+url.split('/')[-1]
try:
    if not os.path.exists(root):
        os.mkdir(root)
    if not os.path.exists(path):
        r=requests.get(url)
        with open(path,'wb') as f:
            f.write(r.content)
            f.close()
            print("文件保存成功")
    else:
        print("文件已存在")
except:
    print("爬取失败")
发布了7 篇原创文章 · 获赞 0 · 访问量 17

猜你喜欢

转载自blog.csdn.net/qq_44838702/article/details/104984694
今日推荐