TED Talk Downloader: Python Automation Project With Source Code
200 Projects For Beginners Using Python: Series
Introduction
The project in this article was taken from a series called 200 Projects For Beginners Using Python. You will be able to find more projects belonging to this series on my page. Subscribe to my page to keep up-to-date with this series and more.
For the aspiring data scientist, data analyst, and data enthusiast

Build it! Break it! Build it!
Project: TED Talk Downloader
Description: This Python project creates a script to download TED talk videos. The video downloaded in this project can be found here. For this project, we used a Jupyter notebook and the following libraries & modules: Requests, Beautifulsoup4, Re and Json.
The provided code demonstrates a process to download a TED talk video from a specific URL using Python. It begins by importing the necessary libraries and modules, such as requests for making HTTP requests and BeautifulSoup for HTML parsing. The code checks for the presence of these libraries and installs them if they are not already installed.
Next, the URL of the TED talk video is defined. The code sends a GET request to the URL and retrieves the content of the webpage. The status code of the response is printed to ensure that the request was successful.
Using BeautifulSoup, the HTML content is parsed to extract specific information. In this case, the code searches for a script element with the id “NEXT_DATA” which contains embedded JSON data. The JSON string is extracted and converted into a Python dictionary for further processing.
From the dictionary, the code retrieves the video data, which includes the URL of the MP4 file. The URL is then accessed using another GET request, and the content of the MP4 file is retrieved.
To save the MP4 file locally, the code specifies a file name and opens a file in binary write mode. It writes the content of the MP4 file obtained from the response to the output file. Once the file writing is complete, the TED talk video is successfully saved as “ted_talk_video.mp4” on the local system.
#Import Libraries and Modules
!pip install requestsinstall Request!pip install beautifulsoup4from bs4 import BeautifulSoupimport re import json #Save the URLurl = 'https://www.ted.com/talks/malcolm_gladwell_choice_happiness_and_spaghetti_sauce/'#Get the URL
r = requests.get(url)
print("Download about to start")#Get the content from the URL
r.content#Get the status
r.status_code#Save the content of the URL to soup
soup = BeautifulSoup(r.content, "html.parser")#Get specific content using an id, parse a valid JSON string and convert it into a Python Dictionary
next_data_script = soup.find(id="__NEXT_DATA__")#Call next_data_script
next_data_script#Read the id as a string
data_json = next_data_script.string#Call data_json
data_json#Read json data
player_data = json.loads(data_json)['props']['pageProps']['videoData']['playerData']#Call player_data
player_data#Read json data from the url
url_content = json.loads(player_data)['resources']['h264'][0]['file']#Save the content from the url
url_content#Get the content of the MP4 file
mp4_response = requests.get(url_content)#Get the response code
mp4_response.status_code#Save the mp4 file
file_name = 'ted_talk_video.mp4'#Write the content of the MP4 file to the output file
with open(file_name,'wb') as f:
f.write(mp4_response.content)You have successfully downloaded a TED Talk Video! 👏
Final Words 🤗
This article explored a Python automation project that enables users to download TED talk videos using the provided source code. By leveraging libraries like requests and BeautifulSoup, users can automate the process of extracting video URLs and saving the corresponding MP4 files locally. Start downloading TED talks effortlessly with this handy tool!
Thank you for reading this article! 🤗 I really appreciate it!
If you enjoyed this article, you can help me share this knowledge with others by👏clapping, 💬commenting, and be sure to 👤+ follow me.
Wait a second. To write on Medium and earn passive income, use this referral link to become a member. ✏️.
