当前位置:网站首页>Yyds dry goods inventory hand-in-hand teach you the development of Tiktok series video batch Downloader
Yyds dry goods inventory hand-in-hand teach you the development of Tiktok series video batch Downloader
2022-07-04 01:51:00 【Python advanced】
Application demonstration
Hello everyone , I'm Xiaoming . Here we have developed a Tiktok Video Downloader , The opening effect is as follows :

If the local Google viewer has never visited the Tiktok homepage before , Click the start download button and the following output will appear :

At this point, we just need to click Visit Tiktok Homepage , The program will use the local Google viewer to access the Tiktok homepage . Click the download button again :

You can see that this video is a collection video :

Then the program only needs to check the first option to download the whole collection :

In this way, the whole collection can be downloaded at one time :

Development process
First of all, according to the previous article : Extract Google viewer Cookie Five realms of
Read the location of Google Explorer installation and local Tiktok related cookie:
"""
Xiao Ming's code
CSDN Home page :https://blog.csdn.net/as604049322
"""
__author__ = ' Xiaoming '
__time__ = '2022/1/23'
import base64
import json
import os
import sqlite3
import winreg
import win32crypt
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
def load_local_key(localStateFilePath):
" Read chrome Save in json In the document key Proceed again base64 Decoding and DPAPI Decrypt to get the real AESGCM key"
with open(localStateFilePath, encoding='u8') as f:
encrypted_key = json.load(f)['os_crypt']['encrypted_key']
encrypted_key_with_header = base64.b64decode(encrypted_key)
encrypted_key = encrypted_key_with_header[5:]
key = win32crypt.CryptUnprotectData(encrypted_key, None, None, None, 0)[1]
return key
def decrypt_value(key, data):
"AESGCM Decrypt "
nonce, cipherbytes = data[3:15], data[15:]
aesgcm = AESGCM(key)
plaintext = aesgcm.decrypt(nonce, cipherbytes, None).decode('u8')
return plaintext
def fetch_host_cookie(host):
" Get all under the specified domain name cookie"
userDataDir = os.environ['LOCALAPPDATA'] + r'\Google\Chrome\User Data'
localStateFilePath = userDataDir + r'\Local State'
cookiepath = userDataDir + r'\Default\Cookies'
# 97 The version has already Cookies Move to Network Under the table of contents
if not os.path.exists(cookiepath) or os.stat(cookiepath).st_size == 0:
cookiepath = userDataDir + r'\Default\Network\Cookies'
# print(cookiepath)
sql = f"select name,encrypted_value from cookies where host_key like '%.{host}'"
cookies = {}
key = load_local_key(localStateFilePath)
with sqlite3.connect(cookiepath) as conn:
cu = conn.cursor()
for name, encrypted_value in cu.execute(sql).fetchall():
cookies[name] = decrypt_value(key, encrypted_value)
return cookies
def get_chrome_path():
try:
key = winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, r"ChromeHTML\Application")
path = winreg.QueryValueEx(key, "ApplicationIcon")[0]
chrome_path = path[:path.rfind(",")]
return chrome_path
except FileNotFoundError as e:
return
if __name__ == '__main__':
print(fetch_host_cookie("douyin.com"))
print(get_chrome_path())
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
With this tool class , We no longer need to use selenium.
Then the core code of video parsing is as follows :
def get_video_url(url, cookies):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36",
"referer": "https://www.douyin.com"
}
res = requests.get(url, headers=headers, cookies=cookies)
if res.status_code == 200:
RENDER_DATA, = re.findall(
r'<script id="RENDER_DATA" type="application/json">([^<>]+)</script>', res.text)
data = json.loads(unquote(RENDER_DATA))
key = '8' if url.find("collection") != -1 else '34'
try:
detail = data[key]['aweme']['detail']
title = detail['desc']
except Exception as e:
print(f"{url} Invalid , Wrong report key:", e)
return
if not title:
title, = re.findall("<title[^>]+>\s*([^>]+)\s*</title>", res.text)
video_url = urljoin(url, detail['video']['playApi'])
collection_urls = set(re.findall("//www.douyin.com/collection/\d+/\d+", res.text))
collection_urls = [urljoin("https://www.douyin.com", url) for url in collection_urls]
collection_urls.sort(key=lambda s: int(s[s.rfind("/") + 1:]))
collection_title = re.findall("<h2 [^>]+>([^<>]+)</h2>", res.text)[0]
return video_url, title, collection_urls, collection_title
else:
print(' Video link request failed !!!')
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
The core code of video download :
def download_video(video_url, title, folder):
start_time = time.time()
res = requests.get(url=video_url, stream=True)
done_size, total = 0, int(res.headers['content-length'])
chunk_size = 1024 * 1024
title = format_filename(title)
file_size = round(int(res.headers['content-length']) / 1024 / 1024, 2)
basename = f"{title}.mp4"
filename = f"{folder}/{title}.mp4"
if os.path.exists(filename):
print(basename, " Already exists , skip ...")
return
print("-----------------------------------")
print(f' Start downloading files :{basename}\n Current file size :{file_size}MB')
with open(filename, 'wb') as f:
for chunk in res.iter_content(chunk_size):
f.write(chunk)
done_size += len(chunk)
cost_time = time.time() - start_time
yield done_size, cost_time, total
# print(f" speed of progress :{done_size / total:.2%},{done_size / cost_time / 1024 / 1024:.2f}MB/s")
cost_time = time.time() - start_time
print(f' file :{basename} Download complete !\n Time consuming :{cost_time:0.2f} second ')
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
Analysis of video links , You can refer to brother Cai's article :
《 use Python Shake it X No watermark short video download 》
link :https://mp.weixin.qq.com/s/NNVT6IH6dpT0rTeu1-oD6w
UI The core code of interface design is as follows :
layout = [
[sg.Text(' Tiktok video address :', font=(" Regular script ", 12)),
sg.In(key='url', size=(70, 1), text_color="#bb8b59",
default_text="https://www.douyin.com/video/6803929443069988103")],
[sg.Checkbox(' If it is a collection, download the whole collection ', key="download_collection", default=False),
sg.Button(' Start the download '),
sg.Button(' Clear output '),
sg.Button(' Visit Tiktok Homepage '),
sg.Button(' Visit the current address '),
],
[sg.Output(size=(85, 10), key="out", text_color="#15d36a")],
[
sg.ProgressBar(1000, size=(20, 20), key='video_bar', bar_color=("#bb8b59", "#295273")),
sg.Text('000.0MB,00/00\n00:00<00:00', key="message_video"),
sg.ProgressBar(1000, size=(20, 20), key='progressbar', bar_color=("#15d36a", "#295273")),
sg.Text('00.00MB/00.00MB\n00.00MB/s', key="message")
],
[sg.Text(' The output directory :', font=(" Regular script ", 12)),
sg.In(size=(35, 1), key="save_dir"),
sg.FolderBrowse('...', target='save_dir', initial_folder="."),
sg.Checkbox(' After downloading \n Open the directory ', key="open_folder", default=True),
sg.Button(' Open the output directory '),
],
[sg.Text("@ Xiaoming :https://blog.csdn.net/as604049322"), ],
]
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
Program download
The complete code of the tool and the download address of the packaged tool :
https://gitcode.net/as604049322/python_gui/-/tree/master/douyin
- 1.

friends , Practice it quickly ! If in the process of learning , Any problems encountered , Welcome to add my friend , I'll pull you in Python The learning exchange group discusses learning together .
边栏推荐
- Which insurance products can the elderly buy?
- Why is the operation unsuccessful (unresolved) uncaught syntaxerror: invalid or unexpected token (resolved)
- G3 boiler water treatment registration examination and G3 boiler water treatment theory examination in 2022
- Chinese Mitten Crab - current market situation and future development trend
- Mobile phone battery - current market situation and future development trend
- Applet graduation design is based on wechat course appointment registration. Applet graduation design opening report function reference
- Solution to the problem that jsp language cannot be recognized in idea
- Pesticide synergist - current market situation and future development trend
- Chapter 3.4: starrocks data import - Flink connector and CDC second level data synchronization
- Pytoch residual network RESNET
猜你喜欢

Use classname to modify style properties

The automatic control system of pump station has powerful functions and diverse application scenarios

Huawei rip and BFD linkage

When tidb meets Flink: tidb efficiently enters the lake "new play" | tilaker team interview

Huawei cloud micro certification Huawei cloud computing service practice has been stable

TP5 automatic registration hook mechanism hook extension, with a complete case

Rearrangement of tag number of cadence OrCAD components and sequence number of schematic page

Applet graduation project based on wechat selection voting applet graduation project opening report function reference

Maximum entropy model
![When the watch system of Jerry's is abnormal, it is used to restore the system [chapter]](/img/fb/7d4a026260f8817460cc67f06e49ae.jpg)
When the watch system of Jerry's is abnormal, it is used to restore the system [chapter]
随机推荐
The latest analysis of hoisting machinery command in 2022 and free examination questions of hoisting machinery command
Description of setting items of Jerry [chapter]
C import Xls data method summary I (upload files and create Workbooks)
Avoid playing with super high conversion rate in material minefields
MPLS③
MySQL utilise la vue pour signaler les erreurs, Explicit / show ne peut pas être publié; Verrouillage des fichiers privés pour la table sous - jacente
SRCNN:Learning a Deep Convolutional Network for Image Super-Resolution
Difference between value and placeholder
From the 18th line to the first line, the new story of the network security industry
Idea if a class cannot be found, it will be red
Winter vacation daily question -- a single element in an ordered array
Jerry's watch information type table [chapter]
Example 072 calculation of salary it is known that the base salary of an employee of a company is 500 yuan. The amount of software sold by the employee and the Commission method are as follows: Sales
Portable two-way radio equipment - current market situation and future development trend
Solution of cursor thickening
Customize redistemplate tool class
Experimental animal models - current market situation and future development trend
Applet graduation project is based on wechat classroom laboratory reservation applet graduation project opening report function reference
String & memory function (detailed explanation)
C import Xls data method summary II (save the uploaded file to the DataTable instance object)