Use Python to automatically download Miss Douyin videos, you have the final say!

The main reason why I wrote this article is because I read an article.

 

"After using Python to pick up these high-looking goddesses on Douyin, I suddenly became a winner in life." The article briefly describes an engineer using Python+ADB+Goose Factory's AI to pay attention to more than a thousand beautiful young ladies in one night.

 

It fully reflects the difference between college students and workers in the factory. Here I have to say ××× cowhide...

 

I used to think about my life alone in the factory of Novosibirsk, thinking about where I should go.

 

 

I remember that I was also relying on the vibrato to spend that period of suffering.

 

It’s just not as good as the boss above, I’m a beautiful young lady who recognizes artificially...

 

Even now I have registered the AI ​​account of Goose Factory, but I still can't use it.

 

So let’s start with something simple, pay attention to it in advance, and then use Python to automate downloading street video! ! !

 

 

/ 01 / Charles

 

Using Charles to find the video API interface, the specific operation is the same as the previous case on Dangdang, so I won’t go into details.

 

 

Here, you can get the request information of the video by sliding the Douyin App.

 

Through many experiments, I found that the back of the link will keep changing, only the front of the link is always the same, that is, "http://v1-dy", "http://v6-dy", and "http://v9-" dy" unchanged.

 

So when writing a script, you can start with this information as a link.

 

 

/ 02 / mitmproxy

 

Use the mitmdump component in mitmproxy to interface with Python scripts and implement post-monitoring processing with Python.

 

 

Here I just use the script to get the link, and not directly use the script to download the video.

 

Because I run the script in the folder where the mitmdump.exe file is located, the requests module cannot be imported into the script.

 

Don't want to mess with those annoying environment variables, so just get the link.

 

Then go to download the video, the video link needs to be de-duplicated, there may be duplicates.

 

The Python script is as follows.

 

def response(flow): 
    urls = ['http://v1-dy','http://v3-dy','http://v6-dy','http://v9-dy'] 
    # Filter the url and select only the url of the video 
    for url in urls: 
        if url in flow.request.url: 
            print(' Tik Tok 

Video 

') 
            with open('douyin.csv','a+', encoding='utf- 8-sig') as f: 
                f.write(flow.request.url + ' 
')

 

 

/ 03 / epochs

 

Configure Appium parameters for Douyin.

 

 

Click the blue button and the phone will automatically launch the Douyin App!

 

Next, operate the phone, and then click Appium's refresh button to get the element location code.

 

Through this practice, it is found that Appium sometimes cannot obtain the positioning of the elements well, which may be the same as the iframe page on the Web side.

 

Therefore, for the elements that cannot be found, I directly click on the location of the phone screen.

 

Since everyone's mobile phone screens are of different sizes, this parameter will definitely change, so there are drawbacks and cannot be used universally.

 

{Swipe left and right to switch pictures}

 

The general operation is as shown above. The main page picture of UP is missing, please make it up by yourself. The Python code is as follows.

 

import time
import random
from appium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support import expected_conditions as EC


def main():
    # 设置驱动配置
    server = 'http://localhost:4723/wd/hub'
    desired_caps = {
        'platformName': 'Android',
        'deviceName': 'STF_AL00',
        'appPackage': 'com.ss.android.ugc.aweme',
        'appActivity': '.main.MainActivity',
        # 关闭手机软键盘
        'unicodeKeyboard': True,
        'resetKeyboard': True
    } 
    driver = webdriver.Remote(server, desired_caps) 
    wait = WebDriverWait(driver, 60) 
    # Agree to the user privacy agreement, click 
    button_1 = wait.until(EC.presence_of_element_located((By.ID,'com.ss.android.ugc. aweme:id/q6'))) 
    button_1.click() 
    # Forbid phone permission, click 
    button_2 = wait.until(EC.presence_of_element_located((By.ID,'com.android.packageinstaller:id/permission_deny_button'))) 
    button_2 .click() 
    # Disable location permission, click 
    button_3 = wait.until(EC.presence_of_element_located((By.ID,'com.android.packageinstaller:id/permission_deny_button'))) 
    button_3.click() 
    time.sleep(2) 
    # Swipe up to enter the TikTok video playback page 
    TouchAction (driver).press(x=515, y=1200).move_to(x=515, y=1000).release().perform()
    # Here you need to set a longer delay, because 
    Douyin has guided operations and prompts, you need to wait for a while time.sleep(20) 
    # Click on the "like" of 
    Douyin to enter the login interface TouchAction(driver).press( x=950, y=800).release().perform() 
    # Click the password to log in 
    button_4 = wait.until(EC.presence_of_element_located((By.ID,'com.ss.android.ugc.aweme:id/afg' ))) 
    button_4.click() 
    # Enter account 
    button_5 = wait.until(EC.presence_of_element_located((By.ID,'com.ss.android.ugc.aweme:id/ab_'))) 
    button_5.send_keys('you的account') 
    # Enter password 
    button_6 = wait.until(EC.presence_of_element_located((By.ID,'com.ss.android.ugc.aweme:id/aes')))  
    button_6.send_keys('Your password')
    time.sleep(2) 
    # Because it will jump out The soft keyboard will obscure the login button. You need to click the soft keyboard to cancel 
    TouchAction(driver).press(x=980, y=1850).release().perform()
    time.sleep(2) 
    # Click the login button 
    button_7 = wait.until(EC.presence_of_element_located((By.ID,'com.ss.android.ugc.aweme:id/abb'))) 
    button_7.click() 
    time. sleep(2) 
    # Login successfully, enter the 
    TikTok video interface, click on the title bar below "我" TouchAction(driver).press(x=990, y=1850).release().perform() 
    # Enter the personal homepage, click Follow 
    button_8 = wait.until(EC.presence_of_element_located((By.ID,'com.ss.android.ugc.aweme:id/a_7'))) 
    button_8.click() 
    # Enter the attention bar , Click the second concern
    button_9 = wait.until(EC.presence_of_element_located((By.XPATH, '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android .widget.FrameLayout/android.view.ViewGroup/android.widget.LinearLayout/android.support.v7.widget.RecyclerView/android.widget.RelativeLayout[2]/android.widget.RelativeLayout[1]'))) 
    button_9. click() 
    # Enter the main page of UP, click the first video 
    button_10 = wait.until(EC.presence_of_element_located((By.ID,'com.ss.android.ugc.aweme:id/aqm'))) 
    button_10.click () 
    # Keep 
        scrolling down the page until the bottom 
    while True: TouchAction(driver).press(x=515, y=1247).move_to(x=515, y=1026).release().perform() 
        scrolling time.sleep(float(random.randint(5, 10))) 


if __name__ =='__main__': 
    main()

 

To download the video code, you need to de-duplicate the video link.

 

import pandas as pd 
import requests 
import os 

num = 0 
dom = [] 
folder_path = "F:/video/" 
os.makedirs(folder_path) 
df = pd.read_csv('douyin.csv', header=None, names=[" url"]) 

# 
        De-duplicate the link and remove the video link obtained by entering the Douyin 
for i in df['url'][2:]: 
    if i not in dom: dom.append(i) 

# Download the video 
for j in dom: 
    url = j 
    num += 1 
    response = requests.get(url, stream=True) 
    filename = str(num) +'.mp4' 
    with open('F:\video\' + filename,'ab+') as f: 
        f.write(response.content) 
        f.flush() 
        print(filename +'download complete')

 

Finally successfully obtained all the videos of the young ladies ...

 

 

If I used to be in the factory, how good would it be to do this show at that time, haha.

 

In fact, I think more, most girls like to shoot Douyin, but they should not download this kind of operation.

 

Then the opportunity for the little brothers is here to download the vibrato videos of the girls you like.

 

Then I edited a video of the "Most Beautiful Moments" series, isn't this a chance...

 

 

/ 04 / Summary

 

The code is on "GitHub". https://github.com/Tobby-star/douyin

Click on the blue font to  receive the 30 gifted e-books!

 

Guess you like

Origin blog.csdn.net/weixin_43881394/article/details/108996466