requests 爬图片

2018-02-06  本文已影响38人  harukou_ou
import requests as req
import os
url = input('请输入图片地址:')
#url = 'https://img.haomeiwen.com/i2256672/958f31b01695b085.gif'
root = 'e://pic//'
path = root + url.split('/')[-1]
try:
    if not os.path.exists(root):
        os.mkdir(root)
    if not os.path.exists(path):
        r = req.get(url)
        with open(path,'wb') as f:
            f.write(r.content)
            f.close()
            print('文件保存成功')
    else:
        print('文件已存在')
     
except:
    print('爬取错误')
上一篇下一篇

猜你喜欢

热点阅读