python实现简单爬取图片保存到本地

发布时间:2019-09-11 07:46:27编辑:auto阅读(2109)

    import requests
    import os
    url="http://lofter.nos.netease.com/sogou-Y1gxMDFIeFVHeWhCTkZaMEkzYWx1bGR5WEszQTdRTEZPcndxZWo3Q0dwTG1iZF9uVENpM0tCaUJNOXA1bmZkTA.jpg"
    root="F://pics//";
    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("图片保存cg")
      else:
        print("保存失败")
    except:
        print('爬取失败')

    2、在windows终端执行以上代码

    image.png


关键字