单一网站图片保存

#!/usr/bin/python

# -*- coding: UTF-8 -*-

"""请求网页"""

import requests,  re, time, os

headers = {

'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '

'Chrome/80.0.3987.106 Safari/537.36 Edg/80.0.361.54'

}  


# *********************输入想要保存图片的网址***************************

aim_url =

response = requests.get(aim_url, headers=headers)

response.encoding ='gbk'

html = response.text

## 手动指定编码类型,避免乱码


"""解析网页"""

urls = re.findall('

dir_name = re.findall(r'>(.*?\d{1,3}P])', html)[-1]

# r 避免复杂的转义

#  \d{1,3}P    代表 XP,XXP,XXXP 其中x为数字


if not os.path.exists(dir_name):

os.mkdir(dir_name)

for urlin urls:

time.sleep(0.01)

file_name = url.split('/')[-1]

response = requests.get(url, headers=headers)

with open(dir_name +'/' + file_name, 'wb')as f:

f.write(response.content)

你可能感兴趣的:(单一网站图片保存)