一、说明
1、使用requests获取视频列表
2、使用selenium、pyautogui控制chrome打开,关闭,按钮等操作
3、使用IDM手动下载视频
二、代码
# -*- coding:utf-8 -*-
import requests
from selenium import webdriver
import pyautogui
import time
videos_url = []
# xiazai_shipin_coords = 0, 0
kaishi_xiazhai_coords = 0, 0
def get_video_urls(url):
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'
}
session = requests.Session()
req_json = session.get(url=url, headers=headers).json()
create_time = jsonpath(req_json, '$..create_time')
player = jsonpath(req_json, '$..player')
title = jsonpath(req_json, '$..title')
videos_url.extend(player)
def get_image_coords(image):
while True:
coords = pyautogui.locateCenterOnScreen(image)
if coords is not None:
print("坐标是: ", coords)
return coords
def open_Chrome(min_code, max_conde):
# global xiazai_shipin_coords
chrome_Options = webdriver.ChromeOptions()
extension_path = r'C:\Program Files (x86)\Google\Chrome\Application\idm-integration-module.crx'
chrome_Options.add_extension(extension_path)
driver_path = r"C:\Program Files (x86)\Chrome\CentBrowser\Application\chromedriver.exe"
# 启动浏览器,并设置好wait
browser = webdriver.Chrome(executable_path=driver_path, options=chrome_Options)
# 如果不开启一个页面,打开标签会报错
browser.get("https://www.baidu.com")
# 窗口最大化
# browser.maximize_window()
# 改变窗口大小
# browser.set_window_size(1080, 768)
# videos_url 有400多条数据,一次打开太多标签会卡顿
for v_url in videos_url[min_code:max_conde]:
js = "window.open('{}')".format(v_url)
# 调用js 开启新标签
browser.execute_script(js)
# 每开启一个标签停2s
time.sleep(2)
# 等待直到检测到坐标
time.sleep(2)
# xiazai_shipin_coords = get_image_coords('xiazai_shipin.png')
# xiazai_shipin_coords = input("请输入x, y")
# 获取页面所有句柄
all_handles = browser.window_handles
for handle in all_handles[2:]:
# 查询当前页面url
# browser.current_url
# 使用句柄切换页面
browser.switch_to.window(handle)
# 切换标签页面后等待2s
time.sleep(0.1)
# 使用鼠标左键点击坐标中心点
xiazai_shipin_coords = get_image_coords('xiazai_shipin.png')
pyautogui.leftClick(xiazai_shipin_coords)
# 关闭当前标签
browser.close()
# 关闭浏览器
browser.quit()
if __name__ == '__main__':
for i in range(1, 24):
req_url = "https://yuba.douyu.com/wbapi/web/group/videolist?type=2&group_id=869469&page={}".format(i)
get_video_urls(req_url)
print(len(videos_url))
mincode = 0
maxcode = 20
for b in range(0, len(videos_url) % 20):
open_Chrome(mincode, maxcode)
三、问题说明
1、会有获取不到下载按钮的情况
2、IDM点击下载后不能自动开始下载,还需要点击,此操作没有
3、斗鱼会有一些视频已不存在,以及一些漏掉的视频要有对比