I usually go to some professional video websites such as Tencent Video and Youku, and watch movies and TV series on them.One of the advantages of these sites is that they can cache videos, and you can enjoy watching dramas on the commute, such as the subway, because the Internet speed on the subway is usually not very good.
But there are some classic movies or TV series, and these videos are not provided, so we can only watch them on some small movie websites. The resources are there, but the problem is that most of these small movie websites do not have video download function., you can't watch it on the subway.
If you can download these videos and transfer them to your phone, can you watch them offline?
Next, the editor will demonstrate how to use python script to download the video of the small movie website with one click.
The following script can be learned
Note:
It is recommended that you visit the official designated platform or professional video website to support genuine versions
The editor takes the online home that I often watch as an example. This website can find a lot of American TV resources.
First, we use Google Chrome to open a video address, such as https://www.zxzj.fun/video/1529-1-1.html
, press F12
code> Or right-click "Inspect" to open the browser console
Then, click the Select Element button on the far left of the console, or press Ctrl + Shift + C
, select the video area of the web page, and you will see the video
tag
It can be found that the src
attribute value is the video link, and a separate .mp4
video address is used. In principle, this address can be downloaded directly from the browser.Copy this address and open it in the browser, you can right-click to download.But TV dramas usually have many episodes, and it is very cumbersome to manually open the webpage every time - open the console - copy the video address - open the video again - and finally download the video.This is when scripts come in handy, automating the process and simplifying the process of repetitive operations.
The overall idea of the script
selenium
to open a webpageselenium
element selector to find the iframe
where the video
tag is located, switch to iframe
video
tag, and then request the video content#!/usr/bin/python# -*- coding: UTF-8 -*-import requests# Google Chrome Driverfrom selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as EC# sleep module, let the program stop running downfrom time import sleep# Set up Google Chrome driverdriver = webdriver.Chrome()# Manually change to the webpage address of the video you want to downloadurl = 'https://www.zxzj.fun/video/1529-1-1.html'# open the Web pagedriver.get(url)try:# find iframe by element selectoriframe = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, '#playleft iframe')))except:driver.quit()# Get the title of the web page, so that you can visually see the title of the currently downloaded videotitle = driver.find_elements(By.TAG_NAME, 'title')[0].get_attribute('innerHTML')# switch to iframedriver.switch_to.frame(iframe)# Get the video address through the video tagvideo = driver.find_elements(By.TAG_NAME, 'video')[0]video_url = video.get_attribute('src')print('video', video_url)# The video address has been obtained, you can close the browserdriver.quit()# Set the request header informationheaders = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62'}# request video contentvideo_content = requests.get(video_url, headers=headers, stream=True)print("Start download")# video sizecontentLength = int(video_content.headers['content-length'])line = 'Size: %.2fMB'# size conversionline = line %(contentLength/1024/1024)# print the total length of the videoprint(line)# store the length of the downloadeddownSize = 0print('video_name', title)# Fragment downloadwith open(title+'.mp4', "wb") as mp4:for chunk in video_content.iter_content(chunk_size=1024 * 1024):if chunk:mp4.write(chunk)# Record the length of the downloaded video and output the download progress in real timedownSize += len(chunk)print('Progress: {:.2%}'.format(downSize / contentLength), end='\r')print("Download completed")
The above briefly shows the use of python's requests
and selenium
libraries to download a mp4
video, which can be used as a learning case.
There is still a lot of room for improvement
url
I have time to share more interesting and useful python scripts later.