Python crawls a short video of Miss Yin, which will be downloaded automatically today!


The text and pictures of this article are from the Internet, only for learning and communication, and do not have any commercial use. The copyright belongs to the original author. If you have any questions, please contact us in time

The following article is from Tencent cloud Author: Python house owner

(want to learn Python? Python learning exchange group: 1039649593, to meet your needs, information has been uploaded to the group file stream, you can download! There are also a large number of the latest 2020 Python learning materials.)

This article mainly introduces the automatic downloading of the Python based on the tiktok video. The article introduces the detailed code through the example code, which has certain reference value for learning or work.

Mobile phone love is tiktok. Many people love to play tiktok. I also like to watch the little sister who is shaking. Tiktok, as a programmer, how can you watch the little sister shaking your voice easily while programming in front of the computer?

The following three tiktok steps can be used to download your favorite little sister’s love video automatically by using Python.


Charles is an app packet capture tool, which is similar to the function of fildler. Using it, you can get all the network requests and responses that occur in the process of APP running.

After downloading and installing Charles on the computer, you need to configure the certificate, and finally turn on SSL monitoring.

The mobile terminal needs to be in the same network with the computer, such as connecting Wi Fi at home, then using Charles agent in the Wi Fi advanced options of the mobile phone, entering the IP address of the computer, port 8888, and finally naming the certificate.

After the mobile phone is tiktok, the phone will turn on the App and App will be able to get the request information.
Through many experiments, it is found that the back of the link is constantly changing, and only the front of the link is always unchanged, that is, the http://v1-dy 」「 http://v6-dy 」「 http://v9-dy “No change.

So when writing a script, you can use this information as the beginning of the link.

Using mitmdump component of mitmproxy, docking Python script, using Python to realize the processing after listening.

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

Because I run the script in the folder where the mitmdump.exe file is located, I can’t import the requests module into the script.

Do not want to configure environment variables, so only get links.

Use the link to download the video again, the video link needs to be removed, because there may be duplicate.

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 urlfor URL in the video urls:if url  in flow.request. url:print Tiktok ('\n\n \n\n') with open ('douyin.csv','a+', encoding='utf-8-sig') as f:f.write (flow.request.url +'\n')


Appium parameters for the configuration of the tiktok:
Mobile phone tiktok App is activated by clicking the blue button. Then operate the mobile phone, and click the refresh key of appium to get the element location code.

Through this practice, it is found that appium sometimes can’t get the location of elements very well. For the elements that can’t be found, I directly click on the location of the mobile phone screen.

Due to the different screen sizes of mobile phones, this parameter will certainly change, so it has disadvantages and cannot be used in general.

Slide left and right to switch pictures
The general operation is shown in the figure above. The main page of up is missing. Please make it up by yourself. The Python code is as follows:

import timeimport randomfrom appium import webdriverfrom import Byfrom import WebDriverWaitfrom appium.webdriver.common.touch_ action import TouchActionfrom import expected_ Conditions as EC def main(): # set driver configuration server = ' http://localhost :4723/wd/hub'desired_ caps = {'platformName': 'Android','deviceName': 'STF_ Al00 ','apppackage':' ','appactivity': '. Main. Mainactivity', # turn off the phone soft keyboard 'unicodekeyboard': true, 'resetkeyboard': true} driver = webdriver. Remote (server, desired)_ Caps) wait = webdriverwait (driver, 60) # agree to the user privacy agreement and click button_ 1 = wait.until(EC.presence_ of_ element_ located((By.ID, ' aweme:id/q6 ')))button_ 1. Click () # to disable phone access, click button_ 2 = wait.until(EC.presence_ of_ element_ located((By.ID, ' installer:id/permission_ deny_ button')))button_ 2. Click () # disable location permission, click button_ 3 = wait.until(EC.presence_ of_ element_ located((By.ID, ' installer:id/permission_ deny_ button')))button_ () time.sleep (2) tiktok slid upward into the TouchAction video (driver).Press (x=515, y=1200).Move_. Tiktok long time delay tiktok (to, x=515, y=1000).Release ().Perform (), which requires a long delay, because the bootstrap operation and prompts are required, and wait a moment for time.sleep (20) Click to click on the "love" place to enter the login interface TouchAction (driver).Press (x=950, y=800) x=950 () () () () (click) to enter the password. 4 = wait.until(EC.presence_ of_ element_ located((By.ID, ' aweme:id/afg ')))button_ 4. Click () # input account button_ 5 = wait.until(EC.presence_ of_ element_ located((By.ID, ' aweme:id/ab_ '))) button_ 5.send_ Keys ('your account ') ා enter the password button_ 6 = wait.until(EC.presence_ of_ element_ located((By.ID, ' aweme:id/aes ')))button_ 6.send_ Keys ('your password ') time. Sleep (2) # because it will jump out of the soft keyboard and block 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_ 7 = wait.until(EC.presence_ of_ element_ located((By.ID, ' aweme:id/abb ')))button_ () time.sleep (2), the login successfully, enters the shaking video interface, clicks the title bar below, "I" TouchAction (driver).Press (x=990, y=1850).Release ().Perform (), tiktok enters the personal homepage, click on the attention point button_. 8 = wait.until(EC.presence_ of_ element_ located((By.ID, ' aweme:id/a_ 7')))button_ 8. Click () # enter the attention bar and click the second attention 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/[2]/android.widget.RelativeLayout[1]')))button_ 9. Click () # enter the up main page and click the first video button_ 10 = wait.until(EC.presence_ of_ element_ located((By.ID, ' aweme:id/aqm ')))button_ 10. Click () # slide the page down to the bottom while t rue:TouchAction (driver).press(x=515, y=1247).move_ to(x=515, y=1026).release().perform()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 pdimport requestsimport os num = 0dom = []folder_ path = "F:/video/"os.makedirs(folder_ path)df = pd.read_ CSV ('douyin.csv', header=None, names=["URL")] to link and remove the video link that tiktok gets, for I in df['url'][2:]: if I if dom:dom.append (i) Download video for J in dom:url =  jnum += 1response = requests.get(url, stream=True)filename = str(num) + '.mp4'with open('F:\video\' + filename, 'AB +') as F: f.write (response. Content) f.flush() print (file name + 'download complete')


Finally, we successfully obtained all the videos of the ladies:

The above is the whole content of this article, I hope to help you learn

Recommended Today

Dandelion · Jerry technology weekly Vol.21 – technology weekly · React Hooks vs Vue 3 + Composition API

Dandelion · Jerry technology weekly Vol.21 Choose react or Vue, everyone will have their own answer in their heart. There are many reasons to pick the framework of heart water, but when we ask ourselves, can we really evaluate the difference between the two. Perhaps we should return to the original intention and look at […]