来源博客:【Harryの心阁】
效果图如下
代码如下
import requests
import time
import os
import json
import hashlib
# 爬取网站的地址
# 控制台分析请求回复 使用ajax技术,在向下滑动时进行post请求
# 请求地址api 为https://api.zzzmh.cn/bz/getJson
# 获取13位字符的时间戳
# 通过分析可知 access中的数据是由 contentType + location + sign + timestamp 经过哈希算法加密生成的
# 创建文件夹 先判断如果该文件夹下有则跳过 没有则创建
path = os.getcwd() + '/photo'
if not os.path.exists(path):
os.makedirs(path)
# 创建一个类
class simply:
def __init__(self):
self.url = "https://bz.zzzmh.cn/"
self.posturl = 'https://api.zzzmh.cn/bz/getJson'
self.page = input('请输入爬取的页面1-505:')
def main(self, downloadcount):
timestamp = str(int(time.time()*1000))
contentType = 'application/json'
location = 'bz.zzzmh.cn'
sign = '273a3b6b44a285e367af744c37eb30f6'
# 字符串拼接
strs = contentType + location + sign + timestamp
# print(strs)
# 对字符进行哈希加密,解码并转换十六进制
access = hashlib.sha256(strs.encode('utf8')).hexdigest()
# print(access)
header = {
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'zh-CN,zh;q=0.9',
'access': access,
'cache-control': 'no-cache',
'content-length': '30',
'content-type': 'application/json',
'location': 'bz.zzzmh.cn',
'origin': 'https://bz.zzzmh.cn',
'referer': 'https://bz.zzzmh.cn/',
'sign': '273a3b6b44a285e367af744c37eb30f6',
'timestamp': timestamp,
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4462.2 Safari/537.36'
}
data = {
'pageNum': self.page,
'target': 'index'
}
res = requests.post(self.posturl, headers=header, data=json.dumps(data)).json()
# print(res.json())
# 总共爬取到页面的数量为 total为图片总数 pages为爬取到的页面数 datas为当前页面下的图片数据
total = res['result']['total']
pages = res['result']['pages']
# global datas
datas = res['result']['records']
print('总共爬取到页面的数量为:%s\n总共爬取到的页数为:%s' % (total, pages))
print('------------------------------')
print('当前页面的图片数量为:%s' % len(datas))
# 对datas进行遍历分析
counts = datas[:int(downloadcount)]
# print(counts)
# 设置下载的图片索引
a = 0
for i in counts:
# print(a)
# print(i['i'])
url = 'https://w.wallhaven.cc/full/'
# 截取数据图片的数量
# cphoto = i.values[:int(downloadcount)]
f2 = str(i['i'])[0:2]
# print(str(i['i'])[0:2])
pstr = '%s%s/wallhaven-%s.jpg' % (url, f2, str(i['i']))
f = open('photo\\wallhaven-%s.jpg' % (i['i']), mode='wb')
f.write(requests.get(pstr).content)
a += 1
print('正在下载第%s页第%s' % (self.page, a) + '图片!!')
if a == int(downloadcount):
print('%s图片以下载完毕' % a)
if __name__ == '__main__':
# p = simply() 先初始化后在调用一次simply() 后在调用函
downloadcount = input('请输入要下载的图片数量1-120:')
# 调用函数
simply().main(downloadcount)
切勿商用,侵权联系删除