p站多线程下载视频

import re              ######################较稳定,可以爬取多部
import requests as req
from bs4 import BeautifulSoup as bs
import pandas as pd
import time
import threading
from tqdm import tqdm_notebook as tqdm
import os
import winsound

def download(res,title,number):
                try:
                    first_byte=0
                    html=req.get(res,stream=True)
                    length = (html.headers['content-length'])
                    size=int((length))
                    if (size/1024/1024)<0.001:
                        print('------------------------第{}部影片{}爬取失败,请换取url-----------------------------'.format(str(number),title))
                    else:
                        print('--正在爬取-第{}部影片--{}'.format(str(number),title))
                        with open(r'G:\xxxxxx\{}.mp4'.format(title),'wb+') as mp4:
                                pbar=tqdm(total=size, initial=first_byte,unit='B',unit_scale=True,desc=str(number))
                                for chunk in html.iter_content(chunk_size=1024):
                                    if chunk:
                                        mp4.write(chunk)
                                        pbar.update(1024)
                        pbar.close()
                    winsound.Beep(220,500)
                except:
                    print('--{}部影片{}http出现错误'.format(str(number),title))
        
if __name__=='__main__':
    ths=[]
    realurl=[]
    list1=[]
    titles=[]
    list=input('请输入爬取几部:')
    for i in range((int(list))):
        name=input('输入网址:')
        realurl.append(name)
    n=1
    for i in range(len(realurl)):
        url=req.get(url=realurl[i],headers={'User-Agent': 'xxxxxxxx'})
        data=url.text
        x=re.findall('"quality_1080p":"(.*?)"',data,re.S)
        soup=bs(data,'lxml')
        y=soup.find('title').text.split('-')[0].replace('/','').replace('*','').replace('<','').replace('>','').replace(':','').replace('?','').replace('|','').replace('\\','')
#         print(titles)
        if len(x)==0:
            x=re.findall('"quality_720p":"(.*?)"',data,re.S)
            if len(x)==0:
                print('--第{}部{}---dont have 720p,已丢取'.format(str(i+1),y))
                continue
            else:
                print('--准备爬取--第{}部影片-720p-{}'.format(str(i+1),y),'\n')
                while 'ttl'not in x[0]:
#                     print(x[0])
                    url=req.get(url=realurl[i],headers={'User-Agent': 'xxxxxxxxxxxxxxx'})
                    data=url.text
                    x=re.findall('"quality_720p":"(.*?)"',data,re.S)
                    time.sleep(0.2)
                url2=x[0].replace('\\','')
                url3=url2.split('/')
                del url3[1] 
                res=('//'.join(url3))
            
        else:
            print('--准备爬取--第{}部影片-1080p-{}'.format(str(i+1),y),'\n')
            while 'ttl'not in x[0]:
#                     print(x[0])
                url=req.get(url=realurl[i],headers={'User-Agent': 'xxxxxxxxxxxxxxx'})
                data=url.text
                x=re.findall('"quality_1080p":"(.*?)"',data,re.S)
                time.sleep(0.2)
            url2=x[0].replace('\\','')
            url3=url2.split('/')
            del url3[1] 
            res=('//'.join(url3))
            
        list1.append(res)
        titles.append(y)

    for i in range(len(list1)):
            print ('--开始爬取--第{}部影片--{}'.format(i+1,titles[i]),'\n')
            th=threading.Thread(target=download,args=(list1[i],titles[i],i+1,))
            th.start()
            time.sleep(0.1)
            n+=1
            ths.append(th)
    for i in ths:
        i.join()
    print('=================================全部视频已爬取完毕=========================================')
    winsound.Beep(1000,500)

该代码需在代理全局模式下运行,只能爬取1080p和720p视频

注意的点:
1.代码上的headers的user-agent里面的xxxx要替换成自己打开p站网页的user-agent,在chrome浏览器里随便打开p站一个视频的网站,f12检查并且点选手机模式(图中圈出那里,我也不知道是不是叫手机模式),然后复制user-agent.
在这里插入图片描述

  1. with open(r’G:\xxxxxx{}.mp4’.format(title),‘wb+’) as mp4:这串代码中的G:\xxxxxx可以替换成自己想存储在哪个文件下

运行
1.会先弹出爬取几部,
2,然后逐步把视频地址复制黏贴过去即可
在这里插入图片描述

3.开始运行
在这里插入图片描述
运行完成后可以查看文件了
如果哪里能更好,你们也可以提意见,因为本人基础较差。
并且哪里出现敏感信息可以提示一下

在PyQt中使用多线程处理视频需要注意以下几个步骤: 1. 导入PyQt中的和QtGui模块以OpenCV模块。 ```python from PyQt5.QtCore import Qt, QThread, pyqtSignal from PyQt5.QtGui import QImage, QPixmap import cv2 ``` 2. 创建一个继承自QThread的线程类,用于视频处理。在run()方法中打开视频文件并进行处理。 ```python class VideoThread(QThread): change_pixmap_signal = pyqtSignal(QImage) def run(self): cap = cv2.VideoCapture('video.mp4') while cap.isOpened(): ret, frame = cap.read() if ret: rgb_image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) h, w, ch = rgb_image.shape bytes_per_line = ch * w convert_to_qt_format = QImage(rgb_image.data, w, h, bytes_per_line, QImage.Format_RGB888) p = convert_to_qt_format.scaled(640, 480, Qt.KeepAspectRatio) self.change_pixmap_signal.emit(p) else: break cap.release() ``` 3. 在主窗口类中创建一个显示视频的QLabel,并将其放置在布局中。在构造方法中创建VideoThread的实例并连接其change_pixmap_signal信号到更新QLabel的槽函数。 ```python class MainWindow(QWidget): def __init__(self): super().__init__() self.label = QLabel(self) self.label.setAlignment(Qt.AlignCenter) self.label.setMinimumSize(1, 1) vbox = QVBoxLayout() vbox.addWidget(self.label) self.setLayout(vbox) self.thread = VideoThread(self) self.thread.change_pixmap_signal.connect(self.update_image) self.thread.start() def update_image(self, pixmap): self.label.setPixmap(pixmap) ``` 4. 最后,在主程序中创建QApplication实例并显示主窗口。 ```python if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_()) ``` 注意:在多线程中访问Qt GUI元素时应保证线程安全。在上面的例子中,我们使用了信号槽机制来更新QLabel的图像显示。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值