Spider a picture

#图片爬取全代码,名字为网上原定义的名字
import requests
import os
url="https://gss3.bdstatic.com/-Po3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike116%2C5%2C5%2C116%2C38/sign=b93057c84a90f60310bd9415587bd87e/4d086e061d950a7b31ba953d0ad162d9f3d3c9b8.jpg"
root="D://PythonDemo//"
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("爬取失败")

你可能感兴趣的:(Self-taught,Python)