avatarNatasha

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3295

Abstract

<pre><span class="hljs-keyword">import</span> re </pre></div><div id="c233"><pre><span class="hljs-keyword">import</span> json </pre></div><div id="eaf2"><pre><span class="hljs-meta">#Save the URL</span></pre></div><div id="36f9"><pre><span class="hljs-attr">url</span> = <span class="hljs-string">'https://www.ted.com/talks/malcolm_gladwell_choice_happiness_and_spaghetti_sauce/'</span></pre></div><div id="91da"><pre><span class="hljs-comment">#Get the URL</span>

r = requests.<span class="hljs-built_in">get</span>(url) <span class="hljs-built_in">print</span>(<span class="hljs-string">"Download about to start"</span>)</pre></div><div id="6b2a"><pre><span class="hljs-selector-id">#Get</span> the <span class="hljs-attribute">content</span> from the URL r.<span class="hljs-attribute">content</span></pre></div><div id="45d5"><pre><span class="hljs-meta">#Get the status</span> r.status_code</pre></div><div id="ccf7"><pre><span class="hljs-comment">#Save the content of the URL to soup</span> <span class="hljs-attr">soup</span> = BeautifulSoup(r.content, <span class="hljs-string">"html.parser"</span>)</pre></div><div id="8fc9"><pre><span class="hljs-comment">#Get specific content using an id, parse a valid JSON string and convert it into a Python Dictionary</span> next_data_script = soup.<span class="hljs-built_in">find</span>(<span class="hljs-attribute">id</span>=<span class="hljs-string">"NEXT_DATA"</span>)</pre></div><div id="f67f"><pre><span class="hljs-meta">#Call next_data_script</span> next_data_script</pre></div><div id="fc42"><pre>#<span class="hljs-keyword">Read</span> the id <span class="hljs-keyword">as</span> a <span class="hljs-keyword">string</span> data_json = next_data_script.<span class="hljs-keyword">string</span></pre></div><div id="cef0"><pre><span class="hljs-meta">#Call data_json</span> data_json</pre></div><div id="7f38"><pre><span class="hljs-selector-id">#Read</span> json data player_data = json<span class="hljs-selector-class">.loads</span>(data_json)<span class="hljs-selector-attr">[<span class="hljs-string">'props'</span>]</span><span class="hljs-selector-attr">[<span class="hljs-string">'pageProps'</span>]</span><span class="hljs-selector-attr">[<span class="hljs-string">'videoData'</span>]</span><span class="hljs-selector-attr">[<span class="hljs-string">'playerData'</span>]</span></pre></div><div id="70b7"><pre><span class="hljs-meta">#Call player_data</span> player_data</pre></div><div id="f309"><pre><span class="hljs-selector-id">#Read</span> json data from the url url_content = json<span class="hljs-selector-class">.loads</span>(player_data)<span class="hljs-selector-attr">[<span class="hljs-string">'resources'</span>]</span><span class="hljs-selector-attr">[<span class="hljs-string">'h264'</span>]</span><span class="hljs-selector-attr">[0]</span><span class="hljs-selector-attr">[<span class="hljs-string">'file'</span>]</span></pre></div><div id="e9b9"><pre><span class="hljs-comment">#Save the content from the url</span> <span class="hljs-attribute">url_content</span></pre></div><div id="d7db"><pre><span class="hljs-comment">#Get the content of the MP4 file</span> <span class="hljs-attr">mp4_response</span> = requests.get(url_content)</pre></div><div id="3201"><pre>#Get the response <span class="hljs-keyw

Options

ord">code</span> mp4_response.status_code</pre></div><div id="14eb"><pre><span class="hljs-comment">#Save the mp4 file</span> <span class="hljs-attr">file_name</span> = <span class="hljs-string">'ted_talk_video.mp4'</span></pre></div><div id="d6c7"><pre>#<span class="hljs-keyword">Write</span> the content <span class="hljs-keyword">of</span> the MP4 <span class="hljs-keyword">file</span> <span class="hljs-keyword">to</span> the output <span class="hljs-keyword">file</span> <span class="hljs-keyword">with</span> open(file_name,<span class="hljs-string">'wb'</span>) <span class="hljs-keyword">as</span> f: f.<span class="hljs-keyword">write</span>(mp4_response.content)</pre></div><p id="5cef">You have successfully downloaded a TED Talk Video! 👏</p><div id="8ae6" class="link-block"> <a href="https://github.com/natnew/200-Projects-For-Beginners-Using-Python-Series.git"> <div> <div> <h2>GitHub - natnew/200-Projects-For-Beginners-Using-Python-Series</h2> <div><h3>You can't perform that action at this time. You signed in with another tab or window. You signed out in another tab or…</h3></div> <div><p>github.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*a-J0L1aWGgzpj2BI)"></div> </div> </div> </a> </div><figure id="3f17"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*Xgcym2OvshGXTxDo"><figcaption>Photo by <a href="https://unsplash.com/@emilbruckner?utm_source=medium&amp;utm_medium=referral">Emil Bruckner</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h2 id="e459">Final Words 🤗</h2><p id="d823">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!</p><p id="9027">Thank you for reading this article! 🤗 <i>I really appreciate it!</i></p><p id="ea77"><i>If you enjoyed this article, you can help me share this knowledge with others by<b>👏clapping, 💬commenting, and be sure to 👤+ follow me.</b></i></p><p id="3232"><b>Wait a second.</b> <i>To write on Medium and earn passive income, use this <a href="https://medium.com/@natashanewbold/membership">referral link</a> to become a member. ✏️.</i></p><div id="a416" class="link-block"> <a href="https://medium.com/subscribe/@natashanewbold"> <div> <div> <h2>Get an email whenever Natasha publishes.</h2> <div><h3>Get an email whenever Natasha publishes. By signing up, you will create a Medium account if you don't already have one…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*OLS21xUZu1jI117U)"></div> </div> </div> </a> </div></article></body>

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

Python logo. Source: Python.org

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 requests
install Request
!pip install beautifulsoup4
from bs4 import BeautifulSoup
import re 
import json 
#Save the URL
url = '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! 👏

Photo by Emil Bruckner on Unsplash

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. ✏️.

Python
Programming
Automation
Recommended from ReadMedium