pyppeteer拦截器相关问题

本文介绍如何使用pyppeteer在模拟登录后,通过拦截器将GET请求转换为POST并传递参数,以获取受clouflare验证的视频流。重点在于分离登录与流获取,并确保请求处理的灵活性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pyppeteer拦截某个get请求并将其修改为post并传递参数的方法。

由于爬取的网站有cloufare验证,因此选择使用pyppeteer进行模拟登录,然后获取视频流。但是如果登录和获取流写在一起的话耗时很久,所以将登录的部分提取出来保存cookie

由于获流的请求方式为post请求,故将stream网址重置,修改请求方法并增加param。

# -*- coding: UTF-8 -*-
import asyncio
import random
from urllib.parse import unquote
from pyppeteer import launch
from pyppeteer import launcher
import requests
from requests.adapters import HTTPAdapter

import re
import json
# 获取redis里面存储的cookie
def get_redis(name):
    HOST = '45.91.101.88'
    PORT = '6379'
    PASSWORD = 'bet365pass'
    data = {}
    try:
        sr = StrictRedis(host=HOST, port=PORT, password=PASSWORD, decode_responses=True, db=0)
        data = sr.get(name + '_guides')
    except Exception as e:
        print(e)
    finally:
        return data
# 模拟浏览器方法
async def GetStream(stream_id):
    if '--enable-automation' in launcher.DEFAULT_ARGS:
        launcher.DEFAULT_ARGS.remove("--enable-automation")
    browser = await launch(
        {'handleSIGINT': False, 'handleSIGTERM': False, 'handleSIGHUP': False},
        headless=True, dumpio=True, userDataDir=r'./text',
        args=['--no-sandbox', '--disable-setuid-sandbox', '--disable-blink-features=AutomationControlled'])
    web = await browser.createIncognitoBrowserContext()
    page = await web.newPage()
    await page.setViewport({
        "width": 1068,
        "height": 480
    })
    await page.setUserAgent("Linux/Deepin")
    cookies = json.loads(get_redis('cookies'))
    for i in cookies:
        await page.setCookie(i)
    await page.setRequestInterception(True)
    page.on('request', lambda req: asyncio.ensure_future(intercept_request(req, stream_id)))
    response = await page.goto('https://www.365sb.com/streamingapi/v2/stream')
    responseBody = await response.text()
    url = responseBody.split('&')[2].split("=")[1]
    StreamContent(url)
    # 关闭浏览器
    await page.close()
    await browser.close()


# 请求拦截器函数,设置拦截条件并可作修改
async def intercept_request(interceptedRequest:Request, stream_id):
    interceptedRequest.headers["content-type"] = "application/x-www-form-urlencoded"
    param = 'l=10&m={}&t=446&sl=4'.format(stream_id)
    data = {
        'method': 'POST',
        'postData': param,  # 注意格式,格式错误无法重置请求
        'url': interceptedRequest.url,
        'headers': interceptedRequest.headers
    }
    await interceptedRequest.continue_(data)

 关于拦截器的位置,如果拦截器放置到goto后面,则只能拦截到加载页面之后的请求和响应。

请求拦截器拦截完成后一定要await interceptedRequest.continue_(),不然可能造成其他请求被拦截后无法再次加载出来。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值