自用备份 yande.re扒图

版本1.1:tag and page,翻页,报错暂停

import requests
import re
import os

def download_image(image_url, save_path):
    image_filename = image_url.split("/")[-1]
    save_file_path = os.path.join(save_path, image_filename)
    response = requests.get(image_url)
    with open(save_file_path, "wb") as img_file:
        img_file.write(response.content)
    print(f"已下载并保存图片:{save_file_path}")

# 第一页的基本网址
base_url = "https://yande.re/post?page={}&tags={}"

# 输入需要搜索的tags和起始的page_number
tags_input = input("请输入需要搜索的tags:")
page_number = int(input("请输入起始的page_number:"))

# 图片保存路径
save_path = r"C:\Users\naras\Downloads"  # 改成你期望的下载地址

while True:
    try:
        # 生成当前页的网址
        url_1 = base_url.format(page_number, tags_input)

        response = requests.get(url_1)
        source_code = response.text

        pattern = r"https://yande\.re/post/show/\d+"
        matches = re.findall(pattern, source_code)

        if not matches:
            print("未找到以指定开头的链接")
            break

        for url_2 in matches:
            response = requests.get(url_2)
            source_code = response.text

            start_text = "https://files.yande.re/image/"
            end_text2 = "\""

            start_index = source_code.find(start_text)
            if start_index != -1:
                end_index = source_code.find(end_text2, start_index + len(start_text))
                if end_index != -1:
                    image_url = source_code[start_index:end_index]
                    print("找到链接:", image_url)
                    download_image(image_url, save_path)
                else:
                    print("未找到以指定开头的链接")
            else:
                print("未找到以指定开头的链接")

        # 移动到下一页
        page_number += 1

    except Exception as e:
        print(f"出现错误:{e}")
        print(f"当前的page_number值为:{page_number}")
        break
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值