python爬虫笔记(三)requests模块深入—网络图片的爬取和存储

1. 网络图片爬取

import os 
import requests

root = ".//"
url = "https://img2018.cnblogs.com/i-beta/817161/202001/817161-20200116224428592-123074215.png"
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)
        print(r.status_code)
        with open(path, "wb") as f:
            f.write(r.content)
            f.close()
            print("文件保存成功")
    else:
        print("文件已经存在")
        
except:
    print("提取识别")

猜你喜欢

转载自www.cnblogs.com/douzujun/p/12216149.html