不久前,抖音(抖音)宣布有1.5亿活跃用户。说实话,看到这个数据,我大吃一惊。1.5亿韩元,是什么概念?基本上,年轻人有抖音(抖音)账户。
最近,我在github上看到了大量下载抖音(抖音)短视频的项目。感兴趣的可以在github上搜索Jack-Cherish,上面有很多爬虫的小项目。
接下来,我将与:共享源代码
#-*-代码: utf-8-*-
From import Options,Chrome
From import Browser
From contextlib import closing
Import requests、JSON、time、re、OS、sys和time
From bs4 import BeautifulSoup
Class dou yin(对象):
Def _ _ init _ _ (self,宽度=500,高度=300) :
单击“”
下载抖音(抖音)App视频
单击“”
#无头浏览器
Chrome_options=Options()
c(' user-agent=' Mozilla(windows nt 6.1;Wow64) applewebki (khtml、like gecko)chrome safari;)。
=browser (driver _ name=' chrome ',executable _ path=' d 3360/chrome driver ',options=chrome
Defget _ video _ URLs (self、user _ id) :
单击“”
获取视频播放地址
Parameters:
User_ID:查询的用户ID
Returns:
Video_names:视频名称列表
Video_urls:视频链接列表
昵称:用户昵称
单击“”
Video_names=[]
Video_urls=[]
Unique_id=' '
While unique_id!=user_id:
search _ URL=';keyword=% scount=10 type=1 retry _ type=no _ retry iid=17900846586 device _ id=34692364855 AC=
Req=reque (URL=search _ URL,verify=false)
Html=j)
aweme _ count=html[' user _ list '][0][' user _ info '][' aweme _ count ']
uid=html[' user _ list '][0][' user _ info '][' uid ']
NICKNAME=HTML[' USER _ LIST '][0][' USER _ INFO '][' NICKNAME ']
unique _ id=html[' user _ list '][0][' user _ info '][' unique _ id ']
user _ URL=';Max _ cursor=0 count=% s'% (uid,aweme _ count)
Req=reque (URL=user _ URL,verify=false)
Html=
j)i = 1
for each in html['aweme_list']:
share_desc = each['share_info']['share_desc']
if '抖音-原创音乐短视频社区' == share_desc:
video_names.append(str(i) + '.mp4')
i += 1
else:
video_names.append(share_desc + '.mp4')
video_urls.append(each['share_info']['share_url'])
return video_names, video_urls, nickname
def get_download_url(self, video_url):
"""
获得带水印的视频播放地址
Parameters:
video_url:带水印的视频播放地址
Returns:
download_url: 带水印的视频下载地址
"""
req = reque(url = video_url, verify = False)
bf = BeautifulSoup, 'lxml')
script = bf.find_all('script')[-1]
video_url_js = re.findall('var data = \[(.+)\];', str(script))[0]
video_html = j(video_url_js)
download_url = video_html['video']['play_addr']['url_list'][0]
return download_url
def video_downloader(self, video_url, video_name, watermark_flag=True):
"""
视频下载
Parameters:
video_url: 带水印的视频地址
video_name: 视频名
watermark_flag: 是否下载不带水印的视频
Returns:
无
"""
size = 0
if watermark_flag == True:
video_url = (video_url)
else:
video_url = (video_url)
with closing(reque(video_url, stream=True, verify = False)) as response:
chunk_size = 1024
content_size = in['content-length'])
if re == 200:
(' [文件大小]:%0.2f MB\n' % (content_size / chunk_size / 1024))
with open(video_name, "wb") as file:
for data in re(chunk_size = chunk_size):
(data)
size += len(data)
()
(' [下载进度]:%.2f%%' % float(size / content_size * 100) + '\r')
()
def remove_watermark(self, video_url):
"""
获得无水印的视频播放地址
Parameters:
video_url: 带水印的视频地址
Returns:
无水印的视频下载地址
"""
.visit(';)
.find_by_tag('input').fill(video_url)
.find_by_xpath('//button[@class="btn btn-default"]').click()
html = .find_by_xpath('//div[@class="thumbnail"]/div/p')[0].html
bf = BeautifulSoup(html, 'lxml')
return bf.find('a').get('href')
def run(self):
"""
运行函数
Parameters:
None
Returns:
None
"""
()
user_id = input('请输入ID(例如40103580):')
video_names, video_urls, nickname = (user_id)
if nickname not in os.listdir():
os.mkdir(nickname)
print('视频下载中:共有%d个作品!\n' % len(video_urls))
for num in range(len(video_urls)):
print(' 解析第%d个视频链接 [%s] 中,请稍后!\n' % (num+1, video_urls[num]))
if '\\' in video_names[num]:
video_name = video_names[num].replace('\\', '')
elif '/' in video_names[num]:
video_name = video_names[num].replace('/', '')
else:
video_name = video_names[num]
(video_urls[num], os.(nickname, video_name))
print('\n')
print('下载完成!')
def hello(self):
"""
打印欢迎界面
Parameters:
None
Returns:
None
"""
print('*' * 100)
print('\t\t\t\t抖音App视频下载小助手')
print('\t\t作者:Jack Cui')
print('*' * 100)
if __name__ == '__main__':
douyin = DouYin()
douyin.run()
想要 python 和其他语言学习资料的私信我,想要怎么科学上网的也可以私信我,记得回复 python,不然无法给你回信.