Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to crawl a short video on a platform by Python

Shulou Source: shulou.com Published: 2022-06-02 01:08:33 10月02日 Update

This article shows you how Python crawls a short video on a certain platform, which is concise and easy to understand, and can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Basic development environment

Python 3.6

Pycharm

The use of related modules import osimport requests

Install Python and add it to the environment variable, and pip installs the relevant modules you need.

First, determine the demand

Crawl the video content of the funny section.

Second, website data analysis

First of all, we need to make it clear that the loading mode of a good-looking video website is lazy, and you need to slide down the page to load new content.

The loaded content contains the audio playback address and title.

The content is relatively simple, just look at the code.

Import osimport requestsurl = 'https://haokan.baidu.com/videoui/api/videorec?tab=gaoxiao&act=pcFeed&pd=pc&num=20&shuaxin_id=1612592171486'headers = {' user-agent': 'Mozilla/5.0 (Windows NT 10.0 WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'} response = requests.get (url=url, headers=headers) json_data = response.json () videos = json_data ['data'] [' response'] ['videos'] for index in videos: title = index [' title'] play_url = index ['play_url'] video_content = requests.get (url=play_url Headers=headers) .content path = 'video\\' if not os.path.exists (path): os.mkdir (path) with open (path + title + '.mp4', mode='wb') as f: f.write (video_content) print ('saving:', title)

The above content is how Python crawls short videos on a certain platform. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Content video platform skills methods modules environment knowledge websites good-looking funny concise concise code variables addresses that is data data analysis articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Microsoft Shulou Information MySQL Shulou Tech Info