11# -*- coding:utf-8 -*-
2- from splinter .driver .webdriver .chrome import Options , Chrome
3- from splinter .browser import Browser
42from contextlib import closing
53import requests , json , time , re , os , sys , time
6- from bs4 import BeautifulSoup
4+ from datetime import datetime , timezone
75
86class DouYin (object ):
97def __init__ (self , width = 500 , height = 300 ):
@@ -35,23 +33,25 @@ def get_video_urls(self, user_id):
3533}
3634while unique_id != user_id :
3735search_url = 'https://api.amemv.com/aweme/v1/discover/search/?cursor=0&keyword=%s&count=10&type=1&retry_type=no_retry&iid=17900846586&device_id=34692364855&ac=wifi&channel=xiaomi&aid=1128&app_name=aweme&version_code=162&version_name=1.6.2&device_platform=android&ssmix=a&device_type=MI+5&device_brand=Xiaomi&os_api=24&os_version=7.0&uuid=861945034132187&openudid=dc451556fc0eeadb&manifest_version_code=162&resolution=1080*1920&dpi=480&update_version_code=1622' % user_id
38- req = requests .get (url = search_url , verify = False )
36+ req = requests .get (search_url , headers = headers )
3937html = json .loads (req .text )
4038aweme_count = html ['user_list' ][0 ]['user_info' ]['aweme_count' ]
4139uid = html ['user_list' ][0 ]['user_info' ]['uid' ]
4240nickname = html ['user_list' ][0 ]['user_info' ]['nickname' ]
4341unique_id = html ['user_list' ][0 ]['user_info' ]['unique_id' ]
44- user_url = 'https://www.douyin .com/aweme/v1/aweme/post/?user_id=%s&max_cursor=0&count=%s' % (uid , aweme_count )
45- req = requests .get (url = user_url , headers = headers , verify = False )
42+ user_url = 'https://www.amemv .com/aweme/v1/aweme/post/?user_id=%s&max_cursor=0&count=%s' % (uid , aweme_count )
43+ req = requests .get (user_url , headers = headers )
4644html = json .loads (req .text )
47- i = 1
4845for each in html ['aweme_list' ]:
4946share_desc = each ['share_info' ]['share_desc' ]
47+ unix_timestamp = each ['create_time' ]
48+ utc_time = datetime .fromtimestamp (unix_timestamp , timezone .utc )
49+ local_time = utc_time .astimezone ()
50+ tc = local_time .strftime ('%Y-%m-%d-%H-%M-%S' )
5051if '抖音-原创音乐短视频社区' == share_desc :
51- video_names .append (str (i ) + '.mp4' )
52- i += 1
52+ video_names .append (tc + '.mp4' )
5353else :
54- video_names .append (share_desc + '.mp4' )
54+ video_names .append (tc + '-' + share_desc + '.mp4' )
5555video_urls .append (each ['share_info' ]['share_url' ])
5656
5757return video_names , video_urls , nickname
@@ -64,17 +64,15 @@ def get_download_url(self, video_url, watermark_flag):
6464Returns:
6565download_url: 带水印的视频下载地址
6666"""
67- req = requests .get (url = video_url , verify = False )
68- bf = BeautifulSoup (req .text , 'lxml' )
69- script = bf .find_all ('script' )[- 1 ]
70- video_url_js = re .findall ('var data = \[(.+)\];' , str (script ))[0 ]
71- video_html = json .loads (video_url_js )
67+ req = requests .get (video_url )
68+ _playaddr_re = re .compile (r'playAddr: "(.+)",' )
69+ playaddr = _playaddr_re .search (req .text )
7270# 带水印视频
7371if watermark_flag == True :
74- download_url = video_html [ 'video' ][ 'play_addr' ][ 'url_list' ][ 0 ]
72+ download_url = playaddr . group ( 1 )
7573# 无水印视频
7674else :
77- download_url = video_html [ 'video' ][ 'play_addr' ][ 'url_list' ][ 0 ] .replace ('playwm' ,'play' )
75+ download_url = playaddr . group ( 1 ) .replace ('playwm' ,'play' )
7876return download_url
7977
8078def video_downloader (self , video_url , video_name , watermark_flag = False ):
@@ -89,7 +87,7 @@ def video_downloader(self, video_url, video_name, watermark_flag=False):
8987"""
9088size = 0
9189video_url = self .get_download_url (video_url , watermark_flag = watermark_flag )
92- with closing (requests .get (video_url , stream = True , verify = False )) as response :
90+ with closing (requests .get (video_url , stream = True )) as response :
9391chunk_size = 1024
9492content_size = int (response .headers ['content-length' ])
9593if response .status_code == 200 :
@@ -127,7 +125,10 @@ def run(self):
127125video_name = video_names [num ].replace ('/' , '' )
128126else :
129127video_name = video_names [num ]
130- self .video_downloader (video_urls [num ], os .path .join (nickname , video_name ), watermark_flag )
128+ if os .path .isfile (os .path .join (nickname , video_name )):
129+ print ('视频已存在' )
130+ else :
131+ self .video_downloader (video_urls [num ], os .path .join (nickname , video_name ), watermark_flag )
131132print ('\n ' )
132133print ('下载完成!' )
133134
0 commit comments