Python 爬取极简壁纸图片 js反编译

来源博客:【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)

切勿商用,侵权联系删除

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Harry-iu

顺手给小编加个鸡腿????

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值