Using Python to download tiktok without watermark video!

Posted by Cagecrawler on Wed, 22 Dec 2021 04:35:12 +0100

Share and learn about Python crawler, data analysis and data mining.

Recently, we have found a way to download tiktok without watermark.

# Tiktok interface

The "url" parameter value is the link copied from the tiktok.

Python Download

First, let's tiktok link directly to the results.

Proper watermark

Next, open the developer tool of the browser and see the address of the video.

You can find that "playAddr" is the address of the video, copy and access it.

The link will be redirected to the link starting with "v9", but there is still a watermark.

Next is the key point. First, you need to enable your browser to modify UA, the "user agent" often used by crawlers.

I use Mac + Google browser. Just tell me how I modified it.

Please Baidu ~

First, create a folder in your computer's documents.

The path of this folder is as follows.

/Users/star-river/Documents/MyChrome

And run the following code at the terminal in the root directory.

open -n /Applications/Google\ Chrome.app/ --args --disable-web-security --user-data-dir=/Users/star-river/Documents/MyChrome

So my Google browser can successfully replace UA!

Tiktok links are directly accessed, and the results are different from the original ones.

Find the interface in this mode.

The interface that tiktok begins with "item_ids" contains the watermark free video we want.

These are the two links in the list under "play_addr".

The interface starting with "item_ids" has two parameters that we need to obtain in another interface.

In this way, we also know the parameter values of "item_ids" and "dytk".

However, the two links we obtained by directly accessing the browser will not directly appear in the video. It needs to be the same as the above.

Also change the UA. If the link here is accessed with the UA of "iPhone X", it will fail.

Why, little F doesn't know

Change the browser UA to "Responsive" and the link will be redirected.

So the tiktok video without watermark is fix.

But if every video needs to be downloaded like this, it's too troublesome.

So I wrote the code that you can download the video in Python.

import requests
import json
import re

headers = {
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
    'cache-control': 'max-age=0',
    # This seems important
    'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Mobile Safari/537.36',
}


def download(url):
    """
    Tiktok without watermark video
    """
    # Get interface parameters
    html = requests.get(url=url, headers=headers)
    title = re.findall('itemId: "(.*?)",', html.text)[0]
    dytk = re.findall('dytk: "(.*?)" }', html.text)[0]

    # Splice interface
    url_item = 'https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=' + title + '&dytk=' + dytk

    # Get tiktok without watermark video link
    html_item = requests.get(url=url_item, headers=headers)
    # String to dictionary
    content = json.loads(html_item.text)

    # Video interface
    url_video = content['item_list'][0]['video']['play_addr']['url_list'][1]
    response = requests.get(url_video, headers=headers, allow_redirects=True)

    # Get the link to reset back. This is also the download link of waterless video, but it's useless this time
    redirect = response.url
    print(redirect)

    # The video is binary and needs this download method
    video = requests.get(url_video, headers=headers).content
    video_name = "douyin.mp4"
    with open(video_name, 'wb') as f:
        f.write(video)
        f.flush()
    print("Download complete")


if __name__ == '__main__':
    # Tiktok link
    url = 'https://v.douyin.com/XJj85H/'
    download(url)

No watermark video perfect download.

Interface download

Now that you know how to download videos in Python.

So small F wants to make it easier for everyone to download, so it deploys the program to the server.

You only need to download the video through the small F interface. The code is as follows.

from flask import Flask, request, send_file
import requests
import json
import re

app = Flask(__name__)


# Only get method access is accepted
@app.route("/douyin/", methods=["GET"])
def check():
    headers = {
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
        'cache-control': 'max-age=0',
        'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Mobile Safari/537.36',
    }
    # Default return content
    return_dict = {'code': 1, 'result': False, 'msg': 'Request succeeded'}
    # Judge whether the input parameter is empty
    if request.args is None:
        return_dict['return_code'] = '504'
        return_dict['return_info'] = 'Request parameter is null'
        return json.dumps(return_dict, ensure_ascii=False)
    # Get passed in parameters
    get_data = request.args.to_dict()
    url = get_data.get('url')

    # Get interface parameters
    html = requests.get(url=url, headers=headers)
    title = re.findall('itemId: "(.*?)",', html.text)[0]
    dytk = re.findall('dytk: "(.*?)" }', html.text)[0]

    # Splice interface
    url_item = 'https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=' + title + '&dytk=' + dytk

    # Get tiktok without watermark video link
    html_item = requests.get(url=url_item, headers=headers)
    # String to dictionary
    content = json.loads(html_item.text)

    # Get video related information
    # data = {}
    # Description of video
    # data['videoDesc'] = content['item_list'][0]['desc']
    # Video cover picture, small picture
    # data['dynamiCoverUrl'] = content['item_list'][0]['video']['dynamic_cover']['url_list'][0]
    # Video cover picture, large picture
    # data['staticCoverUrl'] = content['item_list'][0]['video']['origin_cover']['url_list'][0]
    # Number of comments on video
    # data['comments'] = content['item_list'][0]['statistics']['comment_count']
    # Video likes
    # data['prise'] = content['item_list'][0]['statistics']['digg_count']

    # Video interface
    url_video = content['item_list'][0]['video']['play_addr']['url_list'][1]
    response = requests.get(url_video, headers=headers, allow_redirects=True)

    # Get the link to reset back. This is also the download link of waterless video, but it's useless this time
    redirect = response.url
    # print(redirect)
    # Video download link
    # data['videoPlayAddr'] = redirect
    # Returns information about the video
    # return_dict['result'] = data
    # Return results
    # return json.dumps(return_dict, ensure_ascii=False)

    video = requests.get(url=redirect, headers=headers).content
    video_name = "douyin.mp4"
    with open(video_name, 'wb') as f:
        f.write(video)
        f.flush()
    return send_file('douyin.mp4')


if __name__ == "__main__":
    # Local debugging
    app.run(debug=True)
    # Deploy Online
    # app.run(host='127.0.0.1', port=443)

If the flash and Requests libraries are installed locally, this program can be run directly.

And you can download the tiktok video without watermark.

# Local interface
http://127.0.0.1:500/douyin/?url=https://v.douyin.com/CoQBx1/

To deploy to the server, you need to use port 443.

end of document

Your favorite collection is my greatest encouragement!
Welcome to follow me, share Python dry goods and exchange Python technology.
If you have any opinions on the article or any technical problems, please leave a message in the comment area for discussion!

Topics: Python Back-end