找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 文档 工具 设计
查看: 61|回复: 0

知乎图片去水印批量下载

[复制链接]

2万

主题

1249

回帖

2万

积分

超级版主

教育辅助界扛把子

附加身份标识
精华
1
热心
7
听众
1
威望
48
贡献
14307
违规
0
书币
49981
注册时间
2020-4-8

论坛元老灌水之王

发表于 2024-7-6 14:03 | 显示全部楼层 |阅读模式
原理:F12看了下代码,定位到data-original-token中的数据,用它替换掉其它图片的文件名,然后显示出了原图。

代码如下:
[Python] 纯文本查看 复制代码
#!/usr/bin/env python
# _*_ coding: utf-8 _*_

import os
import re
import time
import warnings
import requests

warnings.filterwarnings("ignore")

session = requests.session()  # 对全局进行会话实例化

default_headers = {
    'authority': 'www.zhihu.com',
    'pragma': 'no-cache',
    'cache-control': 'no-cache',
    'sec-ch-ua': '"Chromium";v="21", " Not;A Brand";v="99"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'upgrade-insecure-requests': '1',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36',
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'sec-fetch-site': 'same-origin',
    'sec-fetch-mode': 'navigate',
    'sec-fetch-user': '?1',
    'sec-fetch-dest': 'document',
    'referer': 'https://www.zhihu.com/',
    'accept-language': 'zh-CN,zh;q=0.9',
}



def get_resp(method, url, headers=default_headers, timeout=5, **kwargs):
    for i in range(5):  # 最多重试5次
        try:
            response = session.request(method=method, url=url, headers=headers, timeout=timeout, **kwargs)
            if response.status_code == 200:
                return response.text
            else:
                if i == 4:
                    print('【致命错误】接口状态码' + str(response.status_code) + '异常:' + url)
                else:
                    time.sleep(1)
        except:
            if i == 4:
                print('【致命错误】接口连接异常:' + url)
            else:
                time.sleep(1)
    return None

def downloadFile(fileUrl, folderName, fileSaveName):
    folderName = rename(folderName)
    fileSaveName = rename(fileSaveName)
    if not os.path.exists(folderName):
        os.mkdir(folderName)
    for i in range(5):  # 最多重试5次
        try:
            resp1 = session.get(url=fileUrl,headers=default_headers)
            with open(folderName + "/" + fileSaveName, 'wb') as f:
                f.write(resp1.content)
                print(fileSaveName)
            return
        except:
            if i == 4:
                print('【致命错误】文件下载异常:' + fileUrl)
            else:
                time.sleep(1)


def fixname(fileName):
    pattern = r'[\\/:*?"<>|\r\n]+'  # 过滤Windows文件名中的非法字符
    fileName = re.sub(pattern, "_", fileName)
    return fileName

def rename(fileName):
    fileName = re.sub(r'[\\]+', "\", fileName)
    fileName = re.sub(r'[/]+', "/", fileName)
    fileName = re.sub(r'[:]+', ":", fileName)
    fileName = re.sub(r'[*]+', "*", fileName)
    fileName = re.sub(r'[?]+', "?", fileName)
    fileName = re.sub(r'["]+', """, fileName)
    fileName = re.sub(r'[<]+', "<", fileName)
    fileName = re.sub(r'[>]+', ">", fileName)
    fileName = re.sub(r'[|]+', "|", fileName)
    fileName = re.sub(r'[\r\n]+', "_", fileName)
    return fileName


def main():
    while 1 == 1:
        urlkey = str(input("请输入知乎链接:"))
        if urlkey != '':
            resp = get_resp('post', urlkey)
            list = re.findall(r'data-original-token="(.*?)"', resp)
            for i in list:
                url = f'https://pic2.zhimg.com/{i}.png'
                downloadFile(url, '图片', f'{i}.png')


if __name__ == '__main__':
    main()



Great works are not done by strength, but by persistence! 历尽艰辛的飞升者,成了围剿孙悟空的十万天兵之一。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则 需要先绑定手机号


免责声明:
本站所发布的第三方软件及资源(包括但不仅限于文字/图片/音频/视频等仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢某程序或某个资源,请支持正版软件及版权方利益,注册或购买,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To: admin@cdsy.xyz

QQ|Archiver|手机版|小黑屋|城东书院 ( 湘ICP备19021508号-1|湘公网安备 43102202000103号 )

GMT+8, 2024-11-21 17:59 , Processed in 0.037479 second(s), 27 queries .

Powered by Discuz! CDSY.XYZ

Copyright © 2019-2023, Tencent Cloud.

快速回复 返回顶部 返回列表