python 爬取指定图片并将图片下载到指定文件夹

版权声明:CSDN 博主刘康康 https://blog.csdn.net/liiukangkang/article/details/83180579
"""
Version 1.1.0
Author lkk
Email [email protected]
date 2018-10-19 11:34
DESC 下载指定网页的图片到指定文件夹
"""
import requests
import os
from urllib import request
import re
res = request.urlopen("http://www.27270.com/beautiful/")
html = res.read()
with open("picture.html", 'wb') as f:
    f.write(html)
with open("picture.html", "rb") as f:
    msg = r'alt="(.*?)".*?src="(.*?)"'
    result = re.findall(msg, f.read().decode('gbk'))
    for i in result:
        url = ""
        root = "E:\my_test\day10_18\image/"
        path = root + i[0]+'.jpg'
        try:
            if not os.path.exists(root):
                os.mkdir(root)
                if os.path.exists(path):
                    r = requests.get(i[1])
                    # 如果发送了一个错误请求(一个 4XX 客户端错误,或者
                    # 5XX 服务器错误响应),我们可以通过Response.raise_for_status() 来抛出异常:
                    r.raise_for_status()
                    # 使用with语句可以不用自己手动关闭已经打开的文件流
                    with open(path, "wb") as e:  # 开始写文件,wb代表写二进制文件
                        e.write(r.content)
                    print("爬取完成")
            else:
                os.path.exists(path)
                r = requests.get(i[1])
                r.raise_for_status()
                # 使用with语句可以不用自己手动关闭已经打开的文件流
                with open(path, "wb") as e:  # 开始写文件,wb代表写二进制文件
                    e.write(r.content)
                print("爬取完成")
        except Exception as e:
            print("爬取失败:"+str(e))

猜你喜欢

转载自blog.csdn.net/liiukangkang/article/details/83180579
今日推荐