一、说明

1、使用requests获取视频列表
2、使用selenium、pyautogui控制chrome打开,关闭,按钮等操作
3、使用IDM手动下载视频

二、代码

  1. # -*- coding:utf-8 -*-
  2. import requests
  3. from selenium import webdriver
  4. import pyautogui
  5. import time
  6. videos_url = []
  7. # xiazai_shipin_coords = 0, 0
  8. kaishi_xiazhai_coords = 0, 0
  9. def get_video_urls(url):
  10. headers = {
  11. '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'
  12. }
  13. session = requests.Session()
  14. req_json = session.get(url=url, headers=headers).json()
  15. create_time = jsonpath(req_json, '$..create_time')
  16. player = jsonpath(req_json, '$..player')
  17. title = jsonpath(req_json, '$..title')
  18. videos_url.extend(player)
  19. def get_image_coords(image):
  20. while True:
  21. coords = pyautogui.locateCenterOnScreen(image)
  22. if coords is not None:
  23. print("坐标是: ", coords)
  24. return coords
  25. def open_Chrome(min_code, max_conde):
  26. # global xiazai_shipin_coords
  27. chrome_Options = webdriver.ChromeOptions()
  28. extension_path = r'C:\Program Files (x86)\Google\Chrome\Application\idm-integration-module.crx'
  29. chrome_Options.add_extension(extension_path)
  30. driver_path = r"C:\Program Files (x86)\Chrome\CentBrowser\Application\chromedriver.exe"
  31. # 启动浏览器,并设置好wait
  32. browser = webdriver.Chrome(executable_path=driver_path, options=chrome_Options)
  33. # 如果不开启一个页面,打开标签会报错
  34. browser.get("https://www.baidu.com")
  35. # 窗口最大化
  36. # browser.maximize_window()
  37. # 改变窗口大小
  38. # browser.set_window_size(1080, 768)
  39. # videos_url 有400多条数据,一次打开太多标签会卡顿
  40. for v_url in videos_url[min_code:max_conde]:
  41. js = "window.open('{}')".format(v_url)
  42. # 调用js 开启新标签
  43. browser.execute_script(js)
  44. # 每开启一个标签停2s
  45. time.sleep(2)
  46. # 等待直到检测到坐标
  47. time.sleep(2)
  48. # xiazai_shipin_coords = get_image_coords('xiazai_shipin.png')
  49. # xiazai_shipin_coords = input("请输入x, y")
  50. # 获取页面所有句柄
  51. all_handles = browser.window_handles
  52. for handle in all_handles[2:]:
  53. # 查询当前页面url
  54. # browser.current_url
  55. # 使用句柄切换页面
  56. browser.switch_to.window(handle)
  57. # 切换标签页面后等待2s
  58. time.sleep(0.1)
  59. # 使用鼠标左键点击坐标中心点
  60. xiazai_shipin_coords = get_image_coords('xiazai_shipin.png')
  61. pyautogui.leftClick(xiazai_shipin_coords)
  62. # 关闭当前标签
  63. browser.close()
  64. # 关闭浏览器
  65. browser.quit()
  66. if __name__ == '__main__':
  67. for i in range(1, 24):
  68. req_url = "https://yuba.douyu.com/wbapi/web/group/videolist?type=2&group_id=869469&page={}".format(i)
  69. get_video_urls(req_url)
  70. print(len(videos_url))
  71. mincode = 0
  72. maxcode = 20
  73. for b in range(0, len(videos_url) % 20):
  74. open_Chrome(mincode, maxcode)

三、问题说明

1、会有获取不到下载按钮的情况
2、IDM点击下载后不能自动开始下载,还需要点击,此操作没有
3、斗鱼会有一些视频已不存在,以及一些漏掉的视频要有对比