吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2075|回复: 7
收起左侧

[讨论] 写了一半 视频网站打不开了 (欢迎交流)

  [复制链接]
lihu5841314 发表于 2021-6-29 19:50
[Asm] 纯文本查看 复制代码
import requests,re,time,os
import urllib.parse
import urllib.request
from urllib.parse import quote
from lxml  import  etree
'''
目标网站 :电影淘淘  url = https://www.dytt.com/
搜索url = https://www.dytt.com/vodsearch/-------------.html?wd={name}
'''
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36',
'Referer': 'https://www.dytt.com/',
}

#1. 建立搜索
def  sou_suo(url):
    i = 0
    while True:
        try:
            rep = requests.get(url=url,headers=headers,timeout = 30)
            if  rep.status_code == 200 :
                tree = etree.HTML(rep.text)
                next_url ='https://www.dytt.com' + tree.xpath('//div[@class="tableListMain"]//tr/td[1]/a/@href')[0] #注意遇到tbody标签会让xpath失效
                print(next_url)
                return next_url
        except:
                i += 1
                time.sleep(2)
                if i>50:
                    print("无法获得响应,请检查输入的电影或电视剧名称是否正确")
                    break
                print(f"正在尝试{i}连接")
                continue


def  moive_urls(url):  #连续剧就显示选择列表  电影直接获取播放页
    i = 0
    while True:
        try:
            rep = requests.get(url=url, headers=headers, timeout=30)
            if rep.status_code == 200:
                tree = etree.HTML(rep.text)
                li_lis= tree.xpath('//div[@class="bofangList"]//li')    #注意遇到tbody标签会让xpath失效
                dex = len(li_lis)
                print(dex)
                dics = []
                for  li  in li_lis:
                     moive_url = 'https://www.dytt.com'+ li.xpath('./a/@href')[0]
                     moive_title = li.xpath('./a/text()')[0]
                     dic = {
                         'moive_title': moive_title,
                         'moive_url':moive_url,
                     }
                     dics.append(dic)
                     print(dic)
                return dics
        except:
            i += 1
            time.sleep(2)
            if i > 20:
                print("无法获得响应,请检查输入的电影或电视剧名称是否正确")
                break
            print(f"正在尝试{i}连接")
            continue
def xuan_zhe(dics,name):
     print(dics)
     index = input("选择你要下载的集数:")
     video_url = dics[index]['moive_url']
     video_name =dics[index]['moive_title']
     print(name+video_name,"正在进行请求响应")
     dic2 = {
         'video_url':video_url,
         'video_name':video_name
     }
     return dic2
#-----------------------------------------------------------------------------------------------------------------------
# url = 'https://www.dytt.com/vod-play-id-155223-src-3-num-1.html'
"""
获取m3u8(有些视频有一个m3u8,有些有2个,有些加密,有些没有加密)
url的坑主要斜杠最好左斜杠/  右斜杠\会被当成转义  不能获取响应
"""
def  get_response(url):
    i = 0
    while True:  #防止请求 不到  反复请求
        try:
            rep = requests.get(url=url,headers=headers,timeout = 30)
            rep.encoding = rep.apparent_encoding
            if  rep.status_code == 200 :
                return rep
            #     req = urllib.request.Request(url,headers=headers)
            #     rep = urllib.request.urlopen(req,timeout=2)
            #     print(rep.read())  # 获取响应体 二进制字符串
            #     print(rep.read().decode("utf-8"))  # 对响应体进行解码
            #     return rep
        except:
                i += 1
                time.sleep(2)
                if i>50:
                    print("无法获得响应,请重新试试")
                    break
                print(f"正在尝试{i}连接")
                continue

def get_m3u8(response,path):  #获取下载页m3u8
    start_m3u8_url = re.findall(r'"url":"(?P<start_m3u8_url>.*?)","url_next"',response.text)[0].replace("\\","")
    print(start_m3u8_url)
    rep_start_m3u8 = get_response(start_m3u8_url)
    path1 = path + "/start_m3u8.txt"
    with open(path1,'w') as f:
         f.write(rep_start_m3u8.text)
    with open(path1,"r")  as f1:
         urls = []
         for  line   in  f1:
              line = line.strip()
              if line.startswith("#"):
                  continue
              urls.append(line)
         if  len(urls) <5:
             second_m3u8_url = urllib.parse.urljoin(start_m3u8_url,urls[0])
             print(second_m3u8_url)
             rep_second_m3u8 = get_response(second_m3u8_url)
             second_urls=[]
             path2 = path + "/second_m3u8.txt"
             with open(path2, 'w') as f:
                 f.write(rep_second_m3u8.text)
             with open(path2, "r")  as f1:
                 urls = []
                 for line2 in f1:
                     line2 = line2.strip()
                     if line2.startswith("#"):
                         continue
                     urls.append(line2)
                     print(line2)
             return  second_urls
         elif len(urls)>5:
              return urls

def  movie_down(url,path):
      ts_name = url.split("/")[-1]
      rep3 = get_response(url)
      print(ts_name, "正在下载")
      path3 = path +"/"+ ts_name
      with open(path3,"wb") as f3:
            f3.write(rep3.content)
            print(ts_name,"下载完成")

def main():
    name =quote(str(input("请输入需要下载的电影或电视剧名称:")))
    start_url = f'https://www.dytt.com/vodsearch/-------------.html?wd={name}'
    print(start_url)
    next_url = sou_suo(start_url)
    dics = moive_urls(next_url)
    dic2 = xuan_zhe(dics,name)
    if not os.path.exists(name):
        os.mkdir(name)
    response = get_response(dic2['video_url'])
    path = name +"/" +dic2['video_name']
    urls = get_m3u8(response,path)
    for ts_url  in urls:
        movie_down(ts_url, path)

if __name__ == '__main__':
    main()

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
Jedis + 1 + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

lguang 发表于 2021-6-29 20:06
换个IP继续~应该是访问过频被限制了吧
howyouxiu 发表于 2021-6-29 20:11
fanvalen 发表于 2021-6-29 20:13
叶灬凌风 发表于 2021-6-29 20:18
可以从页面正常访问,应该是被反爬虫了
 楼主| lihu5841314 发表于 2021-6-29 20:22
叶灬凌风 发表于 2021-6-29 20:18
可以从页面正常访问,应该是被反爬虫了

应该不是  我在公司可以打开
三滑稽甲苯 发表于 2021-6-29 20:29
IP代{过}{滤}理一下
ldwz 发表于 2021-6-30 01:08
这个站不错。收藏了~
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-26 00:33

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表