Welcome back to our journey in video scraping! In the previous lesson, you learned how to download videos from public Google Drive links using Go. In this lesson, we will expand your skills by downloading videos from LinkedIn using Go's standard library and the powerful open-source tool yt-dlp. You'll learn how to recognize LinkedIn video URLs, validate them, and download publicly accessible video files directly to your local machine.
In this lesson, you will:
- Identify and validate a range of LinkedIn URLs.
- Understand how to use
yt-dlpto download video files from LinkedIn posts. - Download video files from LinkedIn using Go's standard library and
yt-dlp. - Handle temporary files and consider important legal aspects when downloading videos.
Our goal is to download videos from LinkedIn posts using Go. The first step is to recognize valid LinkedIn URLs that point to video content. LinkedIn video URLs can appear in several formats, such as:
- Full activity:
https://www.linkedin.com/feed/update/urn:li:activity:VIDEO_ID - Post-based:
https://www.linkedin.com/posts/USERNAME_activity-VIDEO_ID
Recognizing these URL structures is essential for starting the download process. Once a valid URL is detected, we will use yt-dlp to fetch and download the video file.
Before you can use the code in this lesson, you need to have yt-dlp installed on your system. yt-dlp is a command-line program that can download videos from a wide variety of sites, including LinkedIn.
Why use yt-dlp?
LinkedIn does not provide direct video file links in the page HTML, and the URLs can be protected or obfuscated. yt-dlp is a robust, open-source tool that can extract and download videos from many platforms, handling authentication, cookies, and video formats for you.
To install yt-dlp, run one of the following commands in your terminal:
On macOS (using Homebrew):
On Linux (using pip):
On Windows:
- Download the latest Windows executable from the yt-dlp releases page.
- Or, if you have Python, run:
Make sure yt-dlp is available in your system's PATH so that Go can invoke it.
If you are using our CodeSignal IDE, yt-dlp is already installed and available for you—no setup required.
The first step in our workflow is to check if a given URL is a LinkedIn video post. This is important to avoid running the downloader on unsupported or invalid URLs.
Here's how you can do this in Go:
Explanation:
- The
isValidDomainhelper function performs secure domain validation by checking for exact matches or valid subdomains, preventing host spoofing attacks (e.g.,linkedin.com.evil.com). - The function parses the input URL.
- It checks if the host is exactly
linkedin.comor a valid subdomain likewww.linkedin.com. - It checks if the path matches known LinkedIn video post patterns.
- Returns
trueif both conditions are met.
This function helps ensure that only valid LinkedIn video URLs are processed in the next step.
After validating the LinkedIn URL, the next step is to download the video. Instead of manually parsing HTML (which is brittle and often fails due to LinkedIn's dynamic content), we'll use the robust open-source tool yt-dlp from Go. This approach works for a wide range of LinkedIn video posts and handles the extraction for you.
Explanation:
- The function first checks if the URL is a valid LinkedIn video post.
- It creates a temporary directory for the download, ensuring your workspace stays clean.
- It constructs a
yt-dlpcommand to download the video, using a robust format selector. - If a
cookies.txtfile is present, it is used for authentication (useful for downloading private or restricted videos).
Being able to download videos from LinkedIn using Go and yt-dlp allows you to collect educational content, access videos offline, and back up your own posts. This approach is robust, maintainable, and leverages the strengths of both Go and the open-source community.
Legal Note:
Always ensure you comply with LinkedIn's terms of service and copyright laws when downloading content. Only download videos you have rights to access or for which you have permission.
Now that you know how to detect and download LinkedIn videos with Go and yt-dlp, try the practice section to reinforce your understanding with hands-on exercises.
