requests笔记4---网络图片爬取及储存

版权声明: https://blog.csdn.net/qq_27469815/article/details/82780548

【Python网络爬虫与信息提取】.MOOC. 北京理工大学

import requests
import os

url = 'jpg_url'
root = r'D:/pic/'
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('爬取失败')

猜你喜欢

转载自blog.csdn.net/qq_27469815/article/details/82780548
今日推荐