"""
Version 1.1.0
Author lkk
Email lkk199404@163.com
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))