requests处理iframe①

 由于测试一半,网站被封,先记录一下

import requests,re
from concurrent.futures import ThreadPoolExecutor


headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36",
            "cookie": "Hm_lvt_b2b84ee87a8af3f8ad100a3da41c8ad0=1636809421,1636895260,1638451748,1638537010; Hm_lpvt_b2b84ee87a8af3f8ad100a3da41c8ad0=1638537010; Hm_lvt_b5093235610ff1922206ff61ddb2a910=1636807512,1636895261,1638451756,1638537011; Hm_lpvt_b5093235610ff1922206ff61ddb2a910=1638537011; prec=94769%3A2322363%2C94800%3A2322547%2C60215%3A2153100%2C60173%3A2151552%2C60204%3A2152665%2C94789%3A2322520%2C94768%3A2322357%2C94753%3A2321496%2C94654%3A2321560",
            "Referer": "http://www.meiju8.cc/movie/94769.html"
        }

# 通过网页找到iframe链接
# 通过请求iframe链接来获取m3u8文件
frame = "https://jx.fqzy.cc/jx.php?url=FQ:a1924f01f35f3e7eaa6198e9dc8bca692bc3e9516ca75ef7607cddc72ee18f92709290948e8be64bcf752598bd3aba674f03b6d2583f72ad7a"

res = requests.get(frame,headers=headers)

# 提取m3u8文件的下载链接
compile = re.compile('"url": "(?P<url>.*?)",',re.S)
m3u8_url = compile.search(res.text).group("url")

# 下载m3u8文件
res_m3u8 = requests.get(m3u8_url)

# 写入m3u8文件
with open("meiju_m3u8.txt","wb") as f:
    f.write(res_m3u8.content)

 部分内容如下,大概有1647行左右

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:11
#EXT-X-MEDIA-SEQUENCE:0
#EXTINF:8.125,
https://lf3-static.bytednsdoc.com/obj/tos-cn-o-0000/e99e5ea6084e4cc0adb9b251c86441af
#EXTINF:8.417,
https://lf6-static.bytednsdoc.com/obj/tos-cn-o-0000/caf770f47a284d9193b44c8ec0740ae3
#EXTINF:4.833,
https://lf9-static.bytednsdoc.com/obj/tos-cn-o-0000/e6520e84b61246a490e0c0426ff268c6
#EXTINF:10.417,
https://lf1-cdn-tos.bytegoofy.com/obj/tos-cn-o-0000/dff661b13c9a48eb8f14361a74c16cc4
#EXTINF:10.417,
https://lf3-cdn-tos.bytegoofy.com/obj/tos-cn-o-0000/f7f44bc9286c42348d9a7fad835fb052
#EXTINF:5.292,
https://lf6-cdn-tos.bytegoofy.com/obj/tos-cn-o-0000/8ff95b1924614fa7a49317e9bbc94f0e
#EXTINF:10.417,
https://lf-cdn-tos.bytescm.com/obj/tos-cn-o-0000/48e90dbdabd143009f16f58a827f6502
#EXTINF:10.417,
https://lf1-cdn-tos.bytescm.com/obj/tos-cn-o-0000/5cbd32d7287a491e9a340146302319a1

读取m3u8文件,提取ts文件的下载链接

 通过请求下载链接来写入数据

# 下载视频使用
video_urls = []
# 合并视频使用
ts_name = []

# 读取全部的链接,添加进列表
with open("meiju_m3u8.txt","r") as f:
    for line in f:
        if line.startswith("#"):
            continue
        line = line.strip()
        video_urls.append(line)

# 下载ts视频
def crawl(url):
    video = requests.get(url)
    name = url.split("/")[-1]+".ts"

    ts_name.apppend(name)

    with open(name, "wb") as f:
        f.write(video.content)

# 创建线程池
with ThreadPoolExecutor(max_workers=10) as pool:
    download = pool.map(crawl,video_urls)

合并视频

ts_name = " ".join(ts_name)
os.system(f"copy {ts_name} > video.mp4")

print("over!!!")

 网站恢复了,

iframe获取方式补充

requests处理iframe②

 

在使用requests库进行网络请求时,可能会遇到各种异常情况,如网络连接错误、超时等。为了保证程序的稳定性,我们需要对这些异常进行处理。 以下是一些处理requests异常的方法: 1. 使用try-except语句捕获异常: ``` import requests try: response = requests.get('http://www.example.com') response.raise_for_status() # 检查响应状态码 except requests.exceptions.RequestException as e: print(e) ``` 在try块中发送请求并检查响应状态码,如果状态码不是200(OK),则会抛出HTTPError异常。在except块中捕获所有requests库抛出的异常,并打印异常信息。 2. 设置超时时间: 可以通过timeout参数设置请求超时时间,避免程序长时间等待无响应的请求。 ``` import requests try: response = requests.get('http://www.example.com', timeout=3) response.raise_for_status() except requests.exceptions.Timeout as e: print('请求超时:', e) except requests.exceptions.RequestException as e: print('请求错误:', e) ``` 在这个例子中,timeout参数设置为3秒,如果请求超过3秒没有响应,则会抛出Timeout异常。 3. 处理网络连接错误: 在进行网络请求时,可能会遇到网络连接错误,如DNS解析失败、连接超时等。可以通过捕获ConnectionError异常来处理这些错误。 ``` import requests try: response = requests.get('http://www.example.com') response.raise_for_status() except requests.exceptions.ConnectionError as e: print('网络连接错误:', e) except requests.exceptions.RequestException as e: print('请求错误:', e) ``` 在这个例子中,如果发生网络连接错误,如DNS解析失败或连接超时等,就会抛出ConnectionError异常。 以上是一些处理requests异常的方法,根据具体情况选择合适的方法进行处理
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值