思路:

  1. 本地创建 anime_list.txt 文件,一行一部动漫名。
  2. 打开爱恋动漫网站(http://www.kisssub.org/)并且加载cookie开启虫洞(显示下载地址)。
  3. 循环搜索anime_list.txt文件中所有动漫名,并且对比本地log.txt,如果没下载过则添加磁力链到downloadlist,然后将页面url保存至log.txt,如果下载过则跳过。
  4. 打开迅雷,将所有downloadlist的磁力链接添加任务到迅雷
  5. 自动搜寻迅雷的’新建任务面板‘,然后后台点击确认下载
  6. 因为是模拟的浏览器,所以为了速度,过滤所有的css,图片以及字体的加载。本来还想过滤所有script,结果过滤后磁力链接就加载不出来了,遂放弃。

运行:

完整代码:

from playwright.sync_api import sync_playwright
from tqdm import tqdm
import time,os,re
from win32com.client import Dispatch
import win32gui, win32ui, win32con, win32api
# kisssub批量下载

cookie = ([{'name': 'HMACCOUNT_BFESS', 'value': '341878EAF40DD0E7', 'domain': '.hm.baidu.com', 'path': '/', 'expires': 1727800127.763209, 'httpOnly': False, 'secure': True, 'sameSite': 'None'}, {'name': 'Hm_lvt_bab0ae65b49b7efd4fde9bf6858ec60b', 'value': '1693240128', 'domain': '.kisssub.org', 'path': '/', 'expires': 1724776127, 'httpOnly': False, 'secure': False, 'sameSite': 'Lax'}, {'name': 'Hm_lpvt_bab0ae65b49b7efd4fde9bf6858ec60b', 'value': '1693240128', 'domain': '.kisssub.org', 'path': '/', 'expires': -1, 'httpOnly': False, 'secure': False, 'sameSite': 'Lax'}, {'name': 'user_script_url', 'value': '%2F%2F1.acgscript.com%2Fscript%2Fmiobt%2F4.js%3F3', 'domain': 'www.kisssub.org', 'path': '/', 'expires': 1727800134.758997, 'httpOnly': False, 'secure': False, 'sameSite': 'Lax'}, {'name': 'user_script_rev', 'value': '20181120.2', 'domain': 'www.kisssub.org', 'path': '/', 'expires': 1727800134.759231, 'httpOnly': False, 'secure': False, 'sameSite': 'Lax'}]
)

filename = 'log.txt'
downloadlist = []
count = 0
try: #创建log.txt文件
    with open(filename,'r',encoding = 'utf-8') as txt:
        pass
except:
    with open(filename, 'w', encoding='utf-8') as txt:
        pass
with open('anime_list.txt', 'r', encoding='utf-8') as txt:
    anime_list = txt.readlines()

def kakunin(url):#确认是否下载过,没下载过则添加到log.txt
    with open('log.txt', 'r+', encoding='utf-8') as txt:
        txt_s = txt.read()
        if url in txt_s:
            return 1
        else:
            txt.write(url + '  ' + time.asctime(time.localtime()) + '\n')
# 用正则表达式去除windows下的特殊字符,这些字符不能用在文件名
def fixname(filename):
    intab = r'[?*/\|.:><]'
    filename = re.sub(intab, "", filename)
    return filename

#过滤图片 css等:
excluded_resource_types = ["stylesheet", "image", "font"]
def block_aggressively(route):
    if (route.request.resource_type in excluded_resource_types):
        route.abort()
    else:
        route.continue_()
# page.route("**/*", block_aggressively)  调用方法:放在goto之前
# page.route("**/*", lambda route: route.abort() if route.request.resource_type == "script"else route.continue_()) 过滤
with sync_playwright() as p:
    chromium = p.chromium
    browser = chromium.launch_persistent_context(headless=True,user_data_dir = r'C:\Users\geass\AppData\Local\Temp\playwright_chromiumdev_profile-66NrjW\Default')
    page = browser.pages[0]
    page.context.add_cookies(cookie)
    page.set_default_timeout(200000)
    page.route("**/*", block_aggressively)
    page1 = browser.new_page()
    page1.set_default_timeout(200000)
    page1.route("**/*", block_aggressively)
    print(anime_list)
    for i in tqdm(anime_list):
        url = 'http://www.kisssub.org/search.php?keyword=' + i
        page.goto(url.strip('\n'))
        title = page.query_selector_all('//*[@id="data_list"]/tr/td[3]/a')
        for j in title:
            url_title = j.get_attribute('href')
            fin = kakunin(url_title)
            if fin != 1:
                count +=1
                url_title = 'http://www.kisssub.org/' +url_title
                page1.goto(url_title)
                downloadlist.append(page1.query_selector('//*[@id="magnet"]').get_attribute('href'))
                title = page1.query_selector('//*[@id="btm"]/div[4]/a[3]').text_content()
                print(title,'磁力加成功,个数为: ',count)
            else:
                break
    print(len(downloadlist),'//',count)
    page.close()

##############迅雷
def xunlei ():
    xunleipath = r'D:\soft\Thunder\Program\Thunder.exe'  #迅雷路径,用于打开迅雷
    os.startfile(xunleipath)
    time.sleep(3)
    thunder = Dispatch('ThunderAgent.Agent64.1')
    for i in downloadlist:
        try:
            thunder.AddTask(i)
            thunder.CommitTasks()
            time.sleep(2)
            print(i)
        except:
            pass
#后台点击
def doClick(cx, cy,child):
    long_position = win32api.MAKELONG(cx, cy)  # 模拟鼠标指针 传送到指定坐标
    win32api.SendMessage(child, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, long_position)  # 模拟鼠标按下
    win32api.SendMessage(child, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, long_position)  # 模拟鼠标弹起
#迅雷所有任务添加后自动点击确认
def xunlei_click():
    while True:
            hWnd = win32gui.FindWindow('Chrome_WidgetWin_0', '新建任务面板')  # 窗口的类名可以用Visual Studio的SPY++工具获取
            child = win32gui.FindWindowEx(hWnd, None, "Chrome_RenderWidgetHostHWND", None)
            print(hWnd)
            if hWnd == 0 :
                    break
            else:
                xunlei_count.add(hWnd)
                doClick(158,561,child) #点击立即下载,根据bt种子文件多少窗口大小有2种,依次点击
                doClick(230, 456,child)
                time.sleep(1)
if downloadlist != []:
    xunlei()
    xunlei_count = set()
    xunlei_click()
    print('添加链接:',count,'下载链接:',len(xunlei_count))

本地环境:

win10ltsc2021
python3.10.11
迅雷11绿色去广告版

不想复制粘贴的直接下载↓

链接: https://pan.baidu.com/s/1huK6ORoHYTv0ySSuOnJUgA?pwd=y3mf 提取码: y3mf