Answer the question
In order to leave comments, you need to log in
How does SaveFrom work?
Hello. I would like to learn and understand in detail how to write a program in python that would download any video from any site. This is not a project! I would like to understand how SaveFrom works for example. Point to materials for study and I would like to parse it straight from the basics.
Answer the question
In order to leave comments, you need to log in
You will not make a universal parser, each site gives content differently.
You need to make a request to the video page and try to find a direct link to the video in the source code and download from it.
Everything looks like this:
0. If the site has an API, it would be better to use it. If the API is available, the rest of the steps are not needed (of course, provided that you can get a direct link to the video)
1. Open the page with the video
import requests
import json
from bs4 import BeautifulSoup
# Ссылка на полную страницу
tiktok_url = 'https://www.tiktok.com/@golden_men_6/video/6803237854542712070'
# Чтобы сайт правильно открылся
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'}
# Запрос на страницу с видео
r = requests.get(tiktok_url, headers=headers, allow_redirects=True)
# парсинг страницы, ссылка на видео лежит в скрипте с id videoObject, который является JSON
soup = BeautifulSoup(r.text, 'html.parser')
script = soup.find('script', attrs={'id': 'videoObject'})
data = json.loads(script.text)
video_url = data['contentUrl']
# запрос на прямую ссылку с видео
r = requests.get(video_url)
#запись
with open('video.mp4', 'wb') as f:
f.write(r.content)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question