Python爬取斗罗大陆全集

打开网址http://www.luoxu.cc/dmplay/C888H-1-265.html

F12打开Fetch/XHR,看到m3u8,ts,一眼顶真,打开index.m3u8

由第一个包含第二个index.m3u8的地址,ctrl+f在源代码中一查index,果然有,不过/前总有个\,这个用replace替换为空

查到第一个index.m3u8,就可以依次找到.ts地址

①爬取原网页,利用re找到第一个index地址

②再利用re找到第二个,然后爬取ts网页内容,添加到mp4文件中

③正则查找要用非贪婪模式,还有记得加上time.sleep()和timeout,以及user-agent要随机取(参考源码),要不然会有connection aborted,被反爬。。

④查看每一集的url的不同点,最后利用线程池一次性爬好几集(这里爬了1-10集),只要你内存够(

源码:

python 复制代码
import requests
from bs4 import BeautifulSoup
import os
import re
import numpy as np
from concurrent.futures import ThreadPoolExecutor
import time
headers=[
    {'user-agent':"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"},
    {'user-agent':"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"},
    {'user-agent':"Mozilla/5.0 (Windows NT 10.0; WOW64) Gecko/20100101 Firefox/61.0"},
    {'user-agent':"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"},
    {'user-agent':"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36"},
    {'user-agent':"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"},
    {'user-agent':"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)"},
    {'user-agent':"Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10.5; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15"},
    {'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'}
]
def fun(url,index):
    r=requests.get(url,headers=np.random.choice(headers),timeout=5)
    index_m3u8=re.findall('https:.*?/index.m3u8',r.text)[0]
    index_m3u8=index_m3u8.replace("\\",'')
    r=requests.get(index_m3u8,np.random.choice(headers),timeout=5)
    index_m3u8=index_m3u8.replace('index.m3u8','')
    index_m3u8=index_m3u8+re.findall('/(.*)?',r.text)[0].split('/')[-3]+'/hls/index.m3u8'
    r=requests.get(index_m3u8,np.random.choice(headers),timeout=5)
    ts=re.findall('https://(.*)\.ts',r.text)
    with open(f"D:/dl/{index}.mp4",'ab') as f:
        for node in ts:
            time.sleep(np.random.randint(1,3))
            node='https://'+node+'.ts'
            r=requests.get(node,np.random.choice(headers),timeout=5)
            f.write(r.content)
    print('ok')
pool=ThreadPoolExecutor(10)
for i in range(1,11):
    url=f'http://www.luoxu.cc/dmplay/C888H-1-{266-i}.html'
    pool.submit(fun,url,i)
相关推荐
郝亚军1 分钟前
IEEE 754 单精度浮点的SEM表示
开发语言·c++·算法
zhangjw348 分钟前
第15篇:Java多线程零基础入门,进程线程、线程创建方式、线程生命周期、线程安全彻底吃透
java·开发语言·面试
蝈理塘(/_\)大怨种9 分钟前
类和对象 (上)
java·开发语言
DeniuHe16 分钟前
sklearn 中所有交叉验证数据集划分方式完整总结
人工智能·python·sklearn
DeniuHe20 分钟前
sklearn中不同交叉验证方法的场景适配
人工智能·python·sklearn
小新11030 分钟前
qt creator 将qInfo的输出日志写入日志文档,方便查看
开发语言·qt
隐于花海,等待花开1 小时前
16.Python 常用第三方库概览 深度解析
python
我材不敲代码1 小时前
Python 函数核心:位置参数与关键字参数详解
java·前端·python
风落无尘1 小时前
第十一章《对齐与安全》 完整学习资料
python·安全·机器学习
hssfscv1 小时前
QT的学习记录1
开发语言·qt·学习