欢迎光临
我们一直在努力

下载 好看视频 的视频

用到的第三方库:requests,lxml

import requests
import os,time
from lxml import etree
 
path = os.path.abspath(os.path.dirname(__file__))+'\\'
 
 
def getCookie():
    global r
    global headers
    headers = {}
    headers['user-agent'] = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36'
    r = requests.session()
    res = r.get('https://haokan.baidu.com/', headers=headers)
 
    headers['Cookie'] = res.headers['Set-Cookie']
 
 
def downloadVideo(url):
 
    getCookie()
    res = r.get(url, headers=headers)
    html = etree.HTML(res.text, etree.HTMLParser())
    result = html.xpath('/html/body/div[1]/div/div[1]/div[1]/div/video/@src')
    videoUrl = result[0]
    result = html.xpath('/html/body/div[1]/div/div[1]/div[2]/h2/text()')
    title = result[0]
    headers['referer'] = url
    VideoBytes = r.get(videoUrl, headers=headers).content
    with open(path+title+'.mp4', 'wb') as f:
        f.write(VideoBytes)
 
    print(title, '下载成功')
 
    time.sleep(2)
 
 
def searchVideo(keyword):
    getCookie()
 
    baseUrl = 'https://haokan.baidu.com/videoui/page/search?pn=1&rn=10&_format=json&tab=video&query='
 
    kw = str(keyword.encode('utf-8'))[2:-1].replace('\\x', '%').upper()
 
    headers['referer'] = 'https://haokan.baidu.com/videoui/page/search?query='+kw
    headers['host'] = 'haokan.baidu.com'
 
    fullUrl = baseUrl+kw
 
    VideoInfoList = r.get(fullUrl, headers=headers).json()[
        'data']['response']['list']
 
    WaitInfoList = []
    for index, i in enumerate(VideoInfoList):
        WaitInfoList.append((i['title'], i['author'], i['duration'], i['read_num'], i['publishTimeText'], i['url']))
        print('\n', '【'+str(index)+'】', i['title'], '\n', '作者:', i['author'], '\n', '时长:', i['duration'], '\n', '播放量:', i['read_num'])
 
    cho = int(input('请输入您需要的视频的序号:'))
 
    url = WaitInfoList[cho][5]
 
    downloadVideo(url)
 
 
def main():
 
    cho = input('''
--------欢迎使用好看视频下载器!---------
请选择功能:
【1】下载视频
【2】搜索并下载视频
请输入序号:
''')
     
    if cho == '1':
        downloadVideo(input('请输入视频链接:'))
    elif cho == '2':
        searchVideo(input('请输入搜索关键词'))
    else:
        print('非法输入!')
        time.sleep(2)
 
main()

 

   

如果文章对你有帮助,欢迎点击上方按钮打赏作者

未经允许不得转载:军哥驿站 » 下载 好看视频 的视频

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

军哥驿站 建站更专业 更方便

网站开发联系我们