目录
1,打印文件名
#打印后缀
import os
def get_f_name(path):
try:
#列出目录中的所有文件和子目录
items=os.listdir(path)
#仅保留文件(排除子目录)
files=[item for item in items if os.path.isfile(os.path.join(path, item))]
return files
except FileNotFoundError:
print(f"目录{path}不存在")
return []
except PermissionError:
print(f"不能访问{path}")
return []
#示例
path=r"C:\Users\yu\Desktop\1" #写你的路径
file_names=get_f_name(path)
#逐行打印文件名
print("所有文件名如下")
for file_name in file_names:
print(file_name)
#不打印后缀
import os
def get_f_name(path):
try:
#列出目录中的所有文件和子目录
items=os.listdir(path)
#仅保留文件(排除子目录)
files=[item for item in items if os.path.isfile(os.path.join(path, item))]
#遍历文件列表,去掉文件后缀
f_name=[item.split('.')[0] for item in files]
return f_name
except FileNotFoundError:
print(f"目录{path}不存在")
return []
except PermissionError:
print(f"不能访问{path}")
return []
#示例
path=r"C:\Users\yu\Desktop\1" #写你的路径
file_names=get_f_name(path)
#逐行打印文件名
print("所有文件名如下")
for file_name in file_names:
print(file_name)
2,打印文件结构
#显示根目录
import os
def print_f_structure(directory, indent=''):
#获取当前目录下所有内容(文件和文件夹)
contents=os.listdir(directory)
#遍历目录中的所有内容
for item in contents:
item_path=os.path.join(directory, item)
#如果是文件夹,则打印路径并递归打印其子文件夹路径和文件
if os.path.isdir(item_path):
print(f"{directory}\\{item}")
print_f_structure(item_path, indent + ' ')
else:
print(f"{indent}{item}")
#示例
directory_path=r"C:\Users\yu\Desktop"#你的路径
print_f_structure(directory_path)
#不显示根目录
import os
def print_f_structure(directory, indent=''):
#获取当前目录下所有内容(文件和文件夹)
contents = os.listdir(directory)
#遍历目录中的所有内容
for item in contents:
item_path = os.path.join(directory, item)
#如果是文件夹,则递归打印该文件夹内部结构
if os.path.isdir(item_path):
print(f"{indent}/{item}")
print_f_structure(item_path, indent + ' ')
else:
print(f"{indent}{item}")
#示例用法
print_f_structure(r"C:\Users\yu\Desktop")
3,批量打开网页
#指定edge浏览器打开
import webbrowser
#下载Edge浏览器的路径,具体路径可调整
webbrowser.register('edge',
None,
webbrowser.BackgroundBrowser("C://Program Files (x86)//Microsoft//Edge//Application//msedge.exe"))
urls = [
]
#批量打开链接
for url in urls:
webbrowser.get('edge').open_new_tab(url)
#默认浏览器打开
import webbrowser
urls = [
]
for url in urls:
webbrowser.open_new_tab(url)
#读取txt中的多个URL
import webbrowser
# 下载Edge浏览器的路径,具体路径可调整
webbrowser.register('edge',
None,
webbrowser.BackgroundBrowser("C://Program Files (x86)//Microsoft//Edge//Application//msedge.exe"))
# 读取txt文件中的URL
with open('1.txt', 'r') as file:
urls = file.readlines()
# 去除每行末尾的换行符并批量打开链接
for url in urls:
webbrowser.get('edge').open_new_tab(url.strip())
4,生成ffmpeg代码
#输入(命名和m3u8链接),生成ffmpeg代码
def get_ffm(cin):
parts=cin.split()
name=' '.join(parts[:-1]) #所有非链接部分作为名称
url=parts[-1] #最后一个以http开头的部分作为链接
if not url.startswith('http'):
print("错误:链接需要以http开头")
return None
command=f'ffmpeg -i "{url}" -c copy {name}.mp4'
return command
#输入
cin=input("输入命名+链接\n")
print()
#输出
ffm=get_ffm(cin)
if ffm:
print(ffm)
#输入多行(命名和m3u8链接)
def get_ffm(cin):
lines=cin.strip().split('\n')
commands=[]
for i in range(0, len(lines), 3):
name=lines[i].strip()
url=lines[i + 1].strip()
if not url.startswith('http'):
print(f"错误:链接需要以http开头 (错误的链接: {url})")
continue
command=f'ffmpeg -i "{url}" -c copy {name}.mp4'
commands.append(command)
return commands
#输入
cin=input("输入命名和链接\n")
#输出
ffm=get_ffm(cin)
for command in ffm:
print(command)
5,批量改文件后缀
#txt————>bat(可自行改代码)
import os
#输入文件夹路径
path=r'C:\Users\yu\Desktop\bat'
#遍历文件夹
for f_name in os.listdir(path):
#检查文件扩展名是否为.txt
if f_name.endswith('.txt'):
old_file=os.path.join(path, f_name)
new_file=os.path.join(path, f_name[:-4] + '.bat')#txt改为bat
os.rename(old_file, new_file)
print(f"{old_file}重命名={new_file}")
else:
print("没找到txt")
6,批量下载库
import subprocess
import sys
import traceback
#列出要安装的库
packages=[
"numpy",
]
print("下载库中,请勿退出-----------")
#显示安装状态
for package in packages:
try:
result=subprocess.check_call([sys.executable,"-m","pip","install",package])
if result==0:
print(f"{package}下载成功")
else:
print(f"{package}下载失败")
except subprocess.CalledProcessError as e:
print(f"{package}下载失败!!!")
except Exception as e:
print(f"安装{package}过程中出现错误: {str(e)}")
print(traceback.format_exc())
7,猫抓
#乄、一键获取m3u8链接
import time
import pyautogui
import pyperclip
import re
import os
#文件路径
url_file=r"C:\Users\yu\Desktop\课件\url.txt"
out_file=r"C:\Users\yu\Desktop\课件\m3u8.txt"
#读URL
with open(url_file,"r") as file:
urls=file.readlines()
#打开输出文件
with open(out_file,"w") as outfile:
for url in urls:
url=url.strip()
if not url:
continue
#打开指定网站
pyautogui.hotkey("win","r")
pyautogui.write(f"chrome {url}\n", interval=0.001)
time.sleep(1)
#点播放键
pyautogui.click(x=949,y=585)
time.sleep(1)
#点猫抓扩展
pyautogui.click(x=1692,y=78)
time.sleep(0.1)
#复制m3u8链接
pyautogui.click(x=1625,y=180)
time.sleep(0.1)
#获取复制的m3u8链接
url_m3u8=pyperclip.paste()
name=re.search(r"/([^/]+)$",url).group(1)
#写入输出文件
outfile.write(f"{name}\n{url_m3u8}\n\n")
#关闭浏览器
pyautogui.hotkey("ctrl","w")
time.sleep(0.1)
#m3u8保存路径
txt=os.path.abspath(out_file)
print(f"m3u8已存到{txt}")
#一、获取当前鼠标坐标
import pyautogui
print(pyautogui.position())
#二、获取m3u8
import time
import pyautogui
import pyperclip
import re
#1,打开指定网站
pyautogui.hotkey('win', 'r')
url="链接"
pyautogui.write(f'chrome {url}\n', interval=0.001)
time.sleep(1)
#2,点播放键
pyautogui.click(x=949, y=585)
time.sleep(1)
#3,点猫抓扩展
pyautogui.click(x=1692, y=78)
time.sleep(0.1)
#4,复制m3u8链接
pyautogui.click(x=1625, y=180)
time.sleep(0.1)
#5,输出
url_m3u8=pyperclip.paste()
name=re.search(r'/([^/]+)$', url).group(1)
print(name)
print(url_m3u8)
#6,关闭
pyautogui.hotkey("ctrl", "w")
time.sleep(0.1)
#三、批量生成脚本
import os
#保存脚本的目录
output_dir=r"C:\Users\yu\Desktop\脚本"
#若目录没有,则创建它
if not os.path.exists(output_dir):
os.makedirs(output_dir)
#读取TXT
with open(r"C:\Users\yu\Desktop\课件\url.txt","r",encoding="utf-8") as file:
urls=[url.strip() for url in file.readlines()]
#模板的内容
template_content="""
import time
import pyautogui
import pyperclip
import re
#1,打开指定网站
pyautogui.hotkey('win','r')
url="{url}"
pyautogui.write(f'chrome {url}\\n',interval=0.001)
time.sleep(1)
#2,点播放键
pyautogui.click(x=949,y=585)
time.sleep(1)
#3,点猫抓扩展
pyautogui.click(x=1692,y=78)
time.sleep(0.1)
#4,复制m3u8链接
pyautogui.click(x=1625,y=180)
time.sleep(0.1)
#5,输出
url_m3u8=pyperclip.paste()
name=re.search(r'/([^/]+)$',url).group(1)
print(name)
print(url_m3u8)
#6,关闭
pyautogui.hotkey("ctrl","w")
time.sleep(0.1)
"""
#替换模板中的url
for i,url in enumerate(urls,start=1):
new_script_content=template_content.format(url=url)
#定义脚本名
new_script_filename=os.path.join(output_dir,f"脚本{i}.py")
#写入新脚本
with open(new_script_filename,"w",encoding="utf-8") as new_script_file:
new_script_file.write(new_script_content)
print(f"生成-{new_script_filename}")
print("生成完毕")
#四、批量打开脚本,并保存列表
import os
import subprocess
#定义脚本所在的目录
script_dir=r"C:\Users\yu\Desktop\脚本"
#定义保存输出结果的文件
output_file=os.path.join(script_dir,"m3u8.txt")
#获取目录中的所有.py文件
script_files=[f for f in os.listdir(script_dir) if f.endswith(".py")]
#打开输出文件
with open(output_file,"w",encoding="utf-8") as outfile:
#依次运行每个脚本
for script in script_files:
script_path=os.path.join(script_dir,script)
try:
#运行脚本并获取输出
result=subprocess.run(["python",script_path],capture_output=True,text=True)
#将输出结果写入文件
outfile.write(result.stdout)
outfile.write("\n")
except Exception as e:
#如果运行脚本时出错,将错误信息写入文件
outfile.write(f"---运行{script}时出错---\n")
outfile.write(str(e))
outfile.write("\n")
print("m3u8已保存到",output_file)
8,利用猫抓下载
#1.py
import time
import pyautogui
import pyperclip
import re
import os
import subprocess
#文件路径
directory=r"C:\Users\yu\Desktop\代码文件夹"
url_file=os.path.join(directory,"1,url.txt")
out_file=os.path.join(directory,"2,m3u8.txt")
bat_file=os.path.join(directory,"3,ffm代码.bat")
def ensure_directory_exists(path):
if not os.path.exists(path):
os.makedirs(path)
def save_urls_to_file(urls):
with open(url_file,"w") as file:
for url in urls:
file.write(url+"\n")
def get_urls_from_user():
urls=[]
print("请输入URL(结束请输入]):")
while True:
url=input()
if url.strip().lower()=="]":
break
urls.append(url.strip())
return urls
def run_automation_script():
#读URL
with open(url_file,"r") as file:
urls=file.readlines()
#打开输出文件
with open(out_file,"w") as outfile:
for url in urls:
url=url.strip()
if not url:
continue
#打开指定网站
pyautogui.hotkey("win","r")
pyautogui.write(f"chrome {url}\n",interval=0.001)
time.sleep(1)
#点播放键
pyautogui.click(x=949,y=585)
time.sleep(1)
#点猫抓扩展
pyautogui.click(x=1692,y=78)
time.sleep(0.1)
#复制m3u8
pyautogui.click(x=1625,y=180)
time.sleep(0.1)
#存m3u8
url_m3u8=pyperclip.paste().strip()
if url_m3u8.startswith("http") and ".m3u8" in url_m3u8:
name=re.search(r"/([^/]+)$",url).group(1)
#写入输出文件
outfile.write(f"{name}\n{url_m3u8}\n\n")
else:
print(f"错误:未能正确获取m3u8链接,跳过此URL:{url}")
#关闭浏览器
pyautogui.hotkey("ctrl","w")
time.sleep(1)
#m3u8保存路径
txt=os.path.abspath(out_file)
print(f"m3u8——存到{txt}")
def get_ffm(cin):
lines=cin.strip().split("\n")
commands=[]
i=0
while i<len(lines):
name=lines[i].strip()
#跳过空行
if not name:
i+=1
continue
if i+1>=len(lines):
print(f"错误:链接和命名不匹配 (命名: {name})")
break
url=lines[i+1].strip()
#跳过空行
if not url:
i+=2
continue
if not url.startswith("http"):
print(f"错误:链接需要以http开头 (错误的链接: {url})")
i+=2
continue
command=f'call ffmpeg -i "{url}" -c copy "{name}.mp4"'
commands.append(command)
i+=2
return commands
def generate_bat_file():
#读取文件中的内容
with open(out_file,"r",encoding="utf-8") as file:
cin=file.read()
#生成FFmpeg命令
ffm=get_ffm(cin)
#保存到输出文件
with open(bat_file,"w",encoding="utf-8") as outfile:
for command in ffm:
outfile.write(command+"\n")
print(f"ffm——存到{os.path.abspath(bat_file)}")
def run_bat_file():
bat_file_path=os.path.abspath(bat_file)
subprocess.Popen(["start",bat_file_path],shell=True)
if __name__=="__main__":
ensure_directory_exists(directory)
urls=get_urls_from_user()
save_urls_to_file(urls)
run_automation_script()
generate_bat_file()
run_bat_file()
#sb.bat
@echo off
:: 获取BAT文件所在的目录
set "script_dir=%~dp0"
:: 切换到BAT文件所在的目录
cd /d "%script_dir%"
:: 运行该目录中的Python脚本1.py
python 1.py
9,为主播加油
import time
import pyautogui
import keyboard
#定义一个退出标志
exit_flag=False
def on_key_event(event):
global exit_flag
if event.name=="esc" and not exit_flag:
print("按下ESC键,结束代码")
exit_flag=True#设置退出标志
keyboard.unhook_all()#移除所有钩子
#0,监听按键
keyboard.hook(on_key_event)
#1,打开网页
pyautogui.hotkey("win","r")
url="链接"
pyautogui.write("chrome "+url+"\n",interval=0.001)
time.sleep(5)
#2,点击评论区
pyautogui.click(1511,957)
time.sleep(2)
#3,循环发送弹幕
while not exit_flag:
#输入弹幕
pyautogui.typewrite("加油")
#按下回车
pyautogui.press("enter")
#等待4秒
time.sleep(4)
print("代码结束")
10,mmbx下载
#根据网页序号下载(速度慢)
import os
import time
import pyautogui
import pyperclip
import requests
#保存视频路径
output_dir=r"C:\Users\yu\Desktop\视频"
#确保输出目录存在
os.makedirs(output_dir,exist_ok=True)
#创建一个会话对象
session=requests.Session()
#请求头
headers={
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/58.0.3029.110 Safari/537.36",
"Referer": "https://your-referer-url.com",#替换为实际的引用页URL,如果需要的话
}
#定义点击和等待的函数
def click_and_wait(x,y,wait_time=0.5,button="left"):
pyautogui.click(x=x,y=y,button=button)
time.sleep(wait_time)
#打开指定网站
def open_website(url):
pyautogui.hotkey("win","r")
pyautogui.write(f"chrome {url}\n",interval=0.0001)
time.sleep(2)
#操作
def f():
#1,点击详情页
click_and_wait(631,386)
#2,复制标题,并输出
pyautogui.click(x=438,y=421)
pyautogui.dragTo(x=1170,y=421,duration=0.5,button="left")
pyautogui.hotkey("ctrl","c")
title=pyperclip.paste().strip()
print(f"{title}")
#3,播放
click_and_wait(400,720,wait_time=5)
#4,点击猫抓
click_and_wait(1734,72,wait_time=0.1)
click_and_wait(1694,142,wait_time=0.1)
#5,复制mp4链接,并输出
pyautogui.hotkey("ctrl","c")
url=pyperclip.paste().strip()
print(f"{url}")
return title,url
#下载视频,并重命名
def download_video(title,url):
print(f"正在下载————{title}")
try:
response=session.get(url,headers=headers,stream=True)
if response.status_code==200:
output_file=os.path.join(output_dir,title + ".mp4")
with open(output_file,"wb") as video_file:
for chunk in response.iter_content(chunk_size=1024):
if chunk:
video_file.write(chunk)
print(f"下载成功————{title}")
else:
print(f"下载失败————{title},错误代码————{response.status_code}")
except Exception as e:
print(f"下载报错————{title}: {e}")
#处理网页的区间
def process_website_range(start,end):
for i in range(start,end + 1):
url=f"https://www.mmbx13.cc/video/?{i}-0-0.html"
open_website(url)
title,download_url=f()
download_video(title,download_url)
#关闭当前标签页
pyautogui.hotkey("ctrl","w")
print()
#选择区间
process_website_range(-1000)
print("已保存")
#ligui上半页,搜索关键字
import time
import pyautogui
import pyperclip
#定义点击和等待的函数
def click_and_wait(x, y, wait_time=0.5, button="left"):
pyautogui.click(x=x, y=y, button=button)
time.sleep(wait_time)
#打开指定网站
def open_website(url):
pyautogui.hotkey("win", "r")
pyautogui.write(f"chrome {url}\n", interval=0.0001)
time.sleep(2)
#搜索关键字
def search_keyword(keyword):
click_and_wait(1438, 143)
time.sleep(1) #确保搜索框已经被激活
pyperclip.copy(keyword)
pyautogui.hotkey("ctrl", "v")
pyautogui.press("enter")
time.sleep(2)
#主函数
def process_keyword(keyword):
search_keyword(keyword)
coordinates = [
(450, 472), (792, 472), (1129, 472), (1427, 472),
(426, 720), (747, 720), (1134, 720), (1464, 720)
]
for coord in coordinates:
#在新标签页中点击链接
click_and_wait(coord[0], coord[1], button="middle")
time.sleep(0.1)
#切换到新标签页
pyautogui.hotkey("ctrl", "tab")
time.sleep(0.001)
#复制文字并输出到控制台
pyautogui.click(x=438, y=421)
pyautogui.dragTo(x=1170, y=421, duration=0.5, button="left")
pyautogui.hotkey("ctrl", "c")
text = pyperclip.paste().strip()
print(text)
#点击下载按钮
click_and_wait(400, 720, wait_time=7)
#点击工具按钮并等待
click_and_wait(1734, 72, wait_time=0.1)
click_and_wait(1692, 140, wait_time=0.1)
#复制信息并输出到控制台
pyautogui.hotkey("ctrl", "c")
info = pyperclip.paste().strip()
print(info, "\n")
#关闭当前标签页并切换回A网页
pyautogui.hotkey("ctrl", "w")
time.sleep(0.001)
#打开网站
open_website("https://www.mmb13.cc/")
process_keyword("")
#关闭浏览器
pyautogui.hotkey("alt", "f4")
#ligui完整界面
import os
import time
import pyautogui
import pyperclip
import requests
#保存视频路径
output_dir=r"C:\Users\yu\Desktop\视频"
#确保输出目录存在
os.makedirs(output_dir, exist_ok=True)
#创建一个会话对象
session=requests.Session()
#请求头
headers={
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/58.0.3029.110 Safari/537.36",
"Referer": "https://your-referer-url.com",#替换为实际的引用页URL,如果需要的话
}
#定义点击和等待的函数
def click_and_wait(x, y, wait_time=0.5, button="left"):
pyautogui.click(x=x, y=y, button=button)
time.sleep(wait_time)
#打开指定网站
def open_website(url):
pyautogui.hotkey("win", "r")
pyautogui.write(f"chrome {url}\n", interval=0.0001)
time.sleep(2)
#搜索关键字
def search_keyword(keyword):
click_and_wait(1438, 143)
time.sleep(1)#确保搜索框已经被激活
pyperclip.copy(keyword)
pyautogui.hotkey("ctrl", "v")
pyautogui.press("enter")
time.sleep(2)
#下载视频,并重命名
def download_video(title, url):
print(f"正在下载————{title}")
try:
response=session.get(url, headers=headers, stream=True)
if response.status_code==200:
output_file=os.path.join(output_dir, title + ".mp4")
with open(output_file, "wb") as video_file:
for chunk in response.iter_content(chunk_size=1024):
if chunk:
video_file.write(chunk)
print(f"下载成功————{title}")
else:
print(f"下载失败————{title},错误代码————{response.status_code}")
except Exception as e:
print(f"下载报错————{title}: {e}")
#主函数
def process_keyword(keyword):
search_keyword(keyword)
page_coordinates=[
(450, 472), (792, 472), (1129, 472), (1427, 472),
(426, 720), (747, 720), (1134, 720), (1464, 720)
]
bottom_coordinates=[
(445, 260), (789, 260), (1125, 260), (1423, 260),
(445, 520), (789, 520), (1125, 520), (1423, 520),
(445, 780), (789, 780), (1125, 780), (1423, 780)
]
next_page_coordinates=[
(901, 947), (950, 941), (992, 941), (1036, 951)
]
for page in range(1, 18):#共17个页面
#处理页面上方的视频
for coord in page_coordinates:
click_and_wait(coord[0], coord[1], button="middle")
time.sleep(0.1)
#切换到新标签页
pyautogui.hotkey("ctrl", "tab")
time.sleep(0.001)
#复制标题并输出到控制台
pyautogui.click(x=438, y=421)
pyautogui.dragTo(x=1170, y=421, duration=0.5, button="left")
pyautogui.hotkey("ctrl", "c")
title=pyperclip.paste().strip()
print(title)
#点击下载按钮
click_and_wait(400, 720, wait_time=7)
#点击工具按钮并等待
click_and_wait(1734, 72, wait_time=0.1)
click_and_wait(1692, 140, wait_time=0.1)
#复制mp4链接并输出到控制台
pyautogui.hotkey("ctrl", "c")
url=pyperclip.paste().strip()
print(url, "\n")
#下载视频
download_video(title, url)
#关闭当前标签页并切换回搜索页面
pyautogui.hotkey("ctrl", "w")
time.sleep(0.001)
#滑动到页面底部
pyautogui.scroll(-10000)
#处理页面底部的视频
for coord in bottom_coordinates:
click_and_wait(coord[0], coord[1], button="middle")
time.sleep(0.1)
#切换到新标签页
pyautogui.hotkey("ctrl", "tab")
time.sleep(0.001)
#复制标题并输出到控制台
pyautogui.click(x=438, y=421)
pyautogui.dragTo(x=1170, y=421, duration=0.5, button="left")
pyautogui.hotkey("ctrl", "c")
title=pyperclip.paste().strip()
print(title)
#点击下载按钮
click_and_wait(400, 720, wait_time=9)
#点击工具按钮并等待
click_and_wait(1734, 72, wait_time=0.1)
click_and_wait(1692, 140, wait_time=0.1)
#复制mp4链接并输出到控制台
pyautogui.hotkey("ctrl", "c")
url=pyperclip.paste().strip()
print(url, "\n")
#下载视频
download_video(title, url)
#关闭当前标签页并切换回搜索页面
pyautogui.hotkey("ctrl", "w")
time.sleep(0.001)
#切换到下一页
if page<4:
click_and_wait(next_page_coordinates[page-1][0], next_page_coordinates[page-1][1], wait_time=2)
else:
click_and_wait(1036, 951, wait_time=2)#对应第5页到第17页
#打开网站
open_website("https://www.mmb13.cc/")
process_keyword("丽柜")
#关闭浏览器
pyautogui.hotkey("alt", "f4")
#根据网页区间,存标题,在浏览器下载
import os
import time
import pyautogui
import pyperclip
#建文件夹
output_dir=r"C:\Users\yu\Desktop\新建文件夹"
#确保输出目录存在
os.makedirs(output_dir,exist_ok=True)
#标题路径
title_file_path=os.path.join(output_dir,"标题.txt")
#定义点击和等待的函数
def click_and_wait(x,y,wait_time=0.5,button="left"):
pyautogui.click(x=x,y=y,button=button)
time.sleep(wait_time)
#打开指定网站
def open_website(url):
pyautogui.hotkey("win","r")
pyautogui.write(f"chrome {url}\n",interval=0.0001)
time.sleep(3)
#操作
def f():
#1,点击详情页
click_and_wait(631,386)
#2,复制标题,并输出
pyautogui.click(x=438,y=421)
pyautogui.dragTo(x=1170,y=421,duration=0.5,button="left")
pyautogui.hotkey("ctrl","c")
title=pyperclip.paste().strip()
print(f"{title}")
#3,播放
click_and_wait(400,720,wait_time=7)
#4,点击猫抓
click_and_wait(1677,76,wait_time=0.5)
click_and_wait(1672,144,wait_time=0.2)
click_and_wait(1378,241,wait_time=0.1)
print(f"{title}开始下载")
#4,标题写入txt
with open(title_file_path, "a", encoding="utf-8") as f:
f.write(f"{title}\n")
#处理网页的区间
def process_website_range(start,end):
for i in range(start,end + 1):
url=f"https://www.mmbx13.cc/video/?{i}-0-0.html"
open_website(url)
f()
#关闭当前标签页
pyautogui.hotkey("ctrl","w")
print()
#选择区间
process_website_range(71,75)
print("已保存")
#3A重命名
import os
import re
#文件路径
txt_path=r"C:\Users\yu\Desktop\新建文件夹\标题.txt"
mp4_path=r"C:\Users\yu\Desktop\新建文件夹"
#读txt
with open(txt_path,"r",encoding="utf-8") as file:
titles=file.readlines()
#标题转为列表
titles=[title.strip() for title in titles]
#找出最小的视频编号
video_numbers=[int(re.match(r"3a(\d+)\.mp4",video_file).group(1)) for video_file in os.listdir(mp4_path) if video_file.endswith(".mp4") and re.match(r"3a(\d+)\.mp4",video_file)]
min_video_number=min(video_numbers) if video_numbers else 0
#遍历文件夹
for video_file in os.listdir(mp4_path):
if video_file.endswith(".mp4"):
match=re.match(r"3a(\d+)\.mp4",video_file)
if match:
video_number=int(match.group(1))
try:
title=titles[video_number-min_video_number]
new_video_name=f"{title}.mp4"
os.rename(os.path.join(mp4_path,video_file),os.path.join(mp4_path,new_video_name))
print(f"重命名《{video_file}》=《{new_video_name}》")
except IndexError:
print(f"没找到{video_number}的标题")
#只在edge下视频
import time
import pyautogui
#定义点击和等待的函数
def click_and_wait(x,y,wait_time=0.5,button="left"):
pyautogui.click(x=x,y=y,button=button)
time.sleep(wait_time)
#打开指定网站
def open_website(url):
pyautogui.hotkey("win","r")
pyautogui.write(f"cmd /c start msedge {url}\n", interval=0.0001)
time.sleep(1)
#操作
def f():
#等猫抓加载
pyautogui.click(450,555)
time.sleep(2)
pyautogui.click(450,555)
time.sleep(2)
#点猫抓
click_and_wait(1716,61,wait_time=0.5)
click_and_wait(1704,124,wait_time=0.2)
click_and_wait(1456,213,wait_time=0.1)
#处理网页的区间
def process_website_range(start,end):
for i in range(start,end + 1):
url=f"https://www.mmbx13.cc/video/?{i}-0-0.html"
open_website(url)
f()
#关闭当前标签页
pyautogui.hotkey("ctrl","w")
print()
#选择区间
process_website_range(69,70)
print("结束")
#edge搜关键字,滑动到底部切下一页,重命名视频并下载
import os
import time
import pyautogui
import pyperclip
import requests
#保存视频路径
output_dir=r"C:\Users\yu\Desktop\视频"
#确保输出目录存在
os.makedirs(output_dir, exist_ok=True)
#创建一个会话对象
session=requests.Session()
#请求头
headers={
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/58.0.3029.110 Safari/537.36",
"Referer": "https://your-referer-url.com",#替换为实际的引用页URL,如果需要的话
}
#定义点击和等待的函数
def click_and_wait(x, y, wait_time=0.5, button="left"):
pyautogui.click(x=x, y=y, button=button)
time.sleep(wait_time)
#打开指定网站
def open_website(url):
pyautogui.hotkey("win", "r")
pyautogui.write(f"cmd /c start msedge {url}\n", interval=0.0001)
time.sleep(2)
#搜索关键字
def search_keyword(keyword):
click_and_wait(1395,121)
time.sleep(1)#确保搜索框已经被激活
pyperclip.copy(keyword)
pyautogui.hotkey("ctrl", "v")
pyautogui.press("enter")
time.sleep(2)
#下载视频,并重命名
def download_video(title,output_dir,url):
print(f"正在下载————{title}")
try:
#用requests.Session()确保连接
with requests.Session() as session:
#发送GET请求,禁用SSL证书验证
response=session.get(url,stream=True, verify=False)
if response.status_code == 200:
#确保输出目录存在
if not os.path.exists(output_dir):
os.makedirs(output_dir)
#视频文件的完整路径
output_file=os.path.join(output_dir, title + ".mp4")
#写入视频文件
with open(output_file, 'wb') as video_file:
for chunk in response.iter_content(chunk_size=1024):
if chunk: #过滤掉保持连接的chunk
video_file.write(chunk)
print(f"下载成功————{title}")
else:
print(f"下载失败————{title},错误代码————{response.status_code}")
except Exception as e:
print(f"下载报错————{title}: {e}")
#主函数
def process_keyword(keyword):
search_keyword(keyword)
page_coordinates=[
(437,450),(784,450),(1119,450),(1472,450),
(437,699),(784,699),(1119,699),(1472,699)
]
bottom_coordinates=[
(437,250),(784,250),(1119,250),(1472,250),
(437,504),(784,504),(1119,504),(1472,504),
(437,759),(784,759),(1119,759),(1472,759)
]
next_page_coordinates=[
(910,937), (955,930), (998,935), (1036,935)
]
for page in range(1, 18):#共17个页面
#处理页面上方的视频
for coord in page_coordinates:
click_and_wait(coord[0], coord[1], button="middle")
time.sleep(0.1)
#切换到新标签页
pyautogui.hotkey("ctrl", "tab")
time.sleep(0.001)
#复制标题并输出
pyautogui.click(x=442,y=401)
pyautogui.dragTo(x=1169,y=401,duration=0.5, button="left")
pyautogui.hotkey("ctrl", "c")
title=pyperclip.paste().strip()
print(title)
#点击播放
click_and_wait(379,696,wait_time=5)
#点击猫抓并复制
click_and_wait(1718,58,wait_time=0.1)
click_and_wait(1680,129,wait_time=0.1)
#复制mp4链接并输出到控制台
pyautogui.hotkey("ctrl", "c")
url=pyperclip.paste().strip()
print(url, "\n")
#下载视频
download_video(title,output_dir,url)
#关闭当前标签页并切换回搜索页面
pyautogui.hotkey("ctrl", "w")
#滑动到页面底部
for _ in range(3): #尝试多次滚动
pyautogui.scroll(-10000)
time.sleep(1)
#处理页面底部的视频
for coord in bottom_coordinates:
click_and_wait(coord[0], coord[1], button="middle")
time.sleep(0.1)
#切换到新标签页
pyautogui.hotkey("ctrl", "tab")
time.sleep(0.001)
#复制标题并输出
pyautogui.click(x=442,y=401)
pyautogui.dragTo(x=1169,y=401,duration=0.5, button="left")
pyautogui.hotkey("ctrl", "c")
title=pyperclip.paste().strip()
print(title)
#点击播放
click_and_wait(379,696,wait_time=5)
#点击猫抓并复制
click_and_wait(1718,58,wait_time=0.1)
click_and_wait(1680,129,wait_time=0.1)
#复制mp4链接并输出到控制台
pyautogui.hotkey("ctrl", "c")
url=pyperclip.paste().strip()
print(url, "\n")
#下载视频
download_video(title,output_dir,url)
#关闭当前标签页并切换回搜索页面
pyautogui.hotkey("ctrl", "w")
#切换到下一页
if page<4:
click_and_wait(next_page_coordinates[page-1][0], next_page_coordinates[page-1][1], wait_time=2)
else:
click_and_wait(1036,935,wait_time=2)#对应第5页到第17页
#打开网站
open_website("https://www.mmbx13.cc/")
process_keyword("")
#关闭浏览器
pyautogui.hotkey("alt", "f4")