Python Web Scraping 101: Downloading Videos
The “web scraper” mentioned by the TikTok blogger is actually just using code to download videos, which is faster than saving them manually with right-click options.
Step 1: Install the Tools
Open the terminal (CMD/Terminal) and paste the following command, then press Enter:
pip install requests
If you see Successfully installed requests, it means the installation is complete.
pipis the Python installation tool, andrequestsis a library that helps you download files.
Step 2: Create a New Python File
Open Notepad or VS Code and create a new file with the name download.py.
Step 3: Tell Python to Use the requests Library
Write the following line in download.py:
import requests
This line tells Python to use the
requestslibrary for the download process.
Step 4: Prepare the Video Link
url = "https://example.com/video.mp4"
Save the direct video link in the variable
url. You can name this variable anything; for example,video_url. The symbol=indicates “store the value on the right side in the variable on the left side”.
Step 5: Download the Video
resp = requests.get(url)
requests.get(url)means to send a request to this link and retrieve the video data.The retrieved data is stored in the variable
resp.respis an abbreviation for “response”.
Step 6: Save the Video to a File
with open("video.mp4", "wb") as f:
f.write(resp.content)
The first line,
open("video.mp4", "wb"), opens a file namedvideo.mp4.wbspecifies that the file should be written in binary mode.The second line,
as f, assigns the opened file to the variableffor easy reference later.The third line,
f.write(resp.content), writes the video data (resp.content) to the filef.
resp.contentcontains the original video data, just like when you save it manually from a browser.
Step 7: Add a Proxy Header to Avoid Blockades
Some websites detect that you’re not using a browser and prevent downloads. Add a proxy header to disguise your request:
headers = {"User-Agent": "Mozilla/5.0"}
Then modify the download line as follows:
resp = requests.get(url, headers=headers)
headersis the request header, telling the server that you are a browser.User-Agentis the name of the browser. Without this, the server will recognize “Python” as a script and may deny you access to the data.
Step 8: Automatically Extract the File Name
filename = url.split("/")[-1]
url.split("/"): Split the URL into several parts using/; for example,["https:", "", "example.com", "video.mp4"].
[-1]: Extract the last part, which isvideo.mp4.This way, the file name can be automatically determined regardless of the actual name of the video.
Complete Code
Put all the code together, and it will look like this in download.py:
import requests
url = "https://example.com/video.mp4"
headers = {"User-Agent": "Mozilla/5.0"}
resp = requests.get(url, headers=headers)
filename = url.split("/")[-1]
with open(filename, "wb") as f:
f.write(resp.content)
print("下载完成:" + filename)
Run the Script
python download.py
The downloaded video file will appear in the current directory.
Summary
| Code | Function |
|---|---|
import requests | Imports the download library |
url = "..." | The video URL |
headers = {"User-Agent": "..."} | Disguises the request as a browser request |
resp = requests.get(url, headers=headers) | Downloads the video |
resp.content | Stores the video data in binary format |
open("文件名", "wb") | Creates the output file |
f.write(resp.content) | Writes the video data to the file |
The essence of web scraping is simple: get the link, download the content, and save it. That’s it!