I'm following a FreeCodeCamp tutorial on how to create a TED Talk video downloader, but it's not working for me... I'm using Google Colab and I've made some changes to the original code. Most of them work, but I'm not getting a video file at the end. Instead, I get a file with no extension that I can't open. Here is the video of the tutorial: https://youtu.be/s8XjEuplx_U?t=2349 (the part about the video downloader starts at 39:09). And here is the URL I'm using (same as the tutorial): https://www.ted.com/talks/sir_ken_robinson_do_schools_kill_creativity.
The tutorial says I should find "mp4" in the page's source code and I should see that it's in a line containing <script>q("talkPage.init"{blablabla) (I wrote blablabla because there is a lot of stuff, it's a huge line). So I should use "talkPage.init" as a way for the code to find the video file.
However, that's not what I see when I check the source code. I've found something I can use, though: <script id="__NEXT_DATA__" type="application/json">, so I figured I could just use "NEXT_DATA" instead of "talkPage.init". Tht seemed to do the trick, but I still get a weird file instead of a video.
Here is the code:
import requests
from bs4 import BeautifulSoup
import re
# Input by the user (modified from original code)
# https://www.ted.com/talks/sir_ken_robinson_do_schools_kill_creativity
url = input("Enter URL:")
r = requests.get(url)
print("Download is about to start...")
# Soup from extracted content
soup = BeautifulSoup(r.content, features="lxml")
for val in soup.findAll("script"):
if(re.search("__NEXT_DATA__", str(val))) is not None:
result = str(val)
# the url must start wit 'https' and end with 'mp4'
result_mp4 = re.search("(?P<url>https?://[^\s]+)(.mp4)", result).group("url")
# split the content (because there's a lot of stuff) and take the first input
mp4_url = result_mp4.split('"')[0]
print("Downloading video from..."+mp4_url)
# Dynamic extraction of the file name
file_name = mp4_url.split("/")[len(mp4_url.split("/"))-1].split('?')[0]
print("Storing video in..."+file_name)
# Extraction of the desired content from mp4_url
r = requests.get(mp4_url)
#saving the output file
with open(file_name, 'wb') as f:
f.write(r.content)
print("Download process finished")
That's the entire code. It "works" because I get a file at the end, but it's not a video. I don't know what I'm missing. Can anybody help please? Thank you!