Python爬取代码

import requests
from requests import RequestException
from lxml import etree
from contextlib import closing
from pyquery import PyQuery as pq
import re
import os
import json
import subprocess
import urllib3
urllib3.disable_warnings()

def main():
    ep=input("请输入ep号")
    baseurl = "https://www.bilibili.com/bangumi/play/ep"+str(ep)
    getVideo(baseurl,ep)
    
    os.system("pause")

def getVideo(baseurl,ep):
    title = str(ep)
    try:
        video_url = "https://upos-sz-mirrorkodo.bilivideo.com/upgcxcode/51/69/118516951/118516951-1-30112.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1608206590&gen=playurl&os=kodobv&oi=2070941197&trid=201e78650999438ab6cea4ab634f9c75p&platform=pc&upsig=664faee92c3062e3ed069b3a2613b689&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=391625457&orderid=0,3&agrr=1&logo=80000000"
        audio_url = "https://upos-sz-mirrorks3.bilivideo.com/upgcxcode/51/69/118516951/118516951_nb2-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1608206590&gen=playurl&os=ks3bv&oi=2070941197&trid=201e78650999438ab6cea4ab634f9c75p&platform=pc&upsig=7126df186e738da8a3ce2f8cbb63b7c4&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=391625457&orderid=0,3&agrr=1&logo=80000000"
        print("下载视频")
        fileDownload(homeurl=baseurl, url=video_url, title=title, typ=0)
        print("下载音频")
        fileDownload(homeurl=baseurl, url=audio_url, title=title, typ=1)
        try:
            combine(title)
        except:
            print("对不起,您的电脑中未安装ffmpeg,不予享受合成服务,您可以尝试使用格式工厂等其他方式\n")
    except Exception:
        vedio_url = temp['data']['durl'][0]['url']
        fileDownload(homeurl=baseurl, url=video_url, title=title, typ=0)

def fileDownload(homeurl, url, title, typ):
    # 添加请求头键值对,写上 refered:请求来源
    headers = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36"
    }
    headers.update({'Referer': homeurl})
    if typ==0:
        print("正在下载视频")
        filename = "./"+title+".flv"
    else:
        print("正在下载音频")
        filename = "./"+title+".mp3"
    res = requests.Session()
    # 指定每次下载1M的数据
    begin = 0
    end = 2048*4096 - 1
    flag = 0
    while True:
        # 添加请求头键值对,写上 range:请求字节范围
        headers.update({'Range': 'bytes=' + str(begin) + '-' + str(end)})
        # 获取视频分片
        res = requests.get(url=url, headers=headers,verify=False)
        if res.status_code != 416:
            # 响应码不为为416时有数据
            begin = end + 1
            end = end + 2048*4096
        else:
            headers.update({'Range': str(end + 1) + '-'})
            res = requests.get(url=url, headers=headers,verify=False)
            flag=1
        with open(filename, 'ab') as fp:
            fp.write(res.content)
            fp.flush()
        if flag==1:
            fp.close()
            break
    print("下载完成!")

def combine(title):
    videopath = "./"+title+".flv"
    videopath = os.getcwd()+"\\"+title+".flv"
    audiopath = "./"+title+".mp3"
    audiopath = os.getcwd()+"\\"+title+".mp3"
    outpath = "./"+title+".flv"
    outpath = os.getcwd()+"\\"+title+".mp4"
    subprocess.call(("C:/ffmpeg-win64-static/bin/ffmpeg -i " + videopath + " -i " + audiopath + " -c copy " + outpath).encode("utf-8").decode("utf-8"),shell=True)
    os.remove(videopath)
    # os.remove(audiopath)

if __name__ == "__main__":
    main()

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 护眼 设计师:闪电赇 返回首页
实付 19.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值