首页 > 编程语言 >python+playwright 学习-83 page.expect_response()捕获网络返回数据

python+playwright 学习-83 page.expect_response()捕获网络返回数据

时间:2023-09-22 21:33:27浏览次数:66  
标签:info playwright python page expect print response

前言

expect_response()方法可以捕获接口返回的数据,在爬取网页数据时非常有用。

expect_response() 使用

官方文档示例

with page.expect_response("https://example.com/resource") as response_info:
    page.get_by_text("trigger response").click()
response = response_info.value
print(response.ok)

# or with a lambda
with page.expect_response(lambda response: response.url == "https://example.com" and response.status == 200) as response_info:
    page.get_by_text("trigger response").click()
response = response_info.value
print(response.ok)

expect_request 参数说明:
url_or_predicate : typing.Union[str, typing.Pattern[str], typing.Callable[["Request"], bool]]
请求URL字符串、正则表达式或接收Request参数的函数对象, 当通过上下文选项提供base_url并且传递的url是路径时,它将通过新的url()构造函数进行合并。
timeout: typing.Optional[float] = None 设置超时时间,默认30秒,单位毫秒, 传递0以禁用超时。可以使用page.set_default_timeout()方法更改默认值。

使用示例

示例代码

from playwright.sync_api import sync_playwright
# 上海悠悠 wx:283340479
# blog:https://www.cnblogs.com/yoyoketang/


with sync_playwright() as p:
    browser = p.chromium.launch(headless=False)
    context = browser.new_context()
    page = context.new_page()
    page.goto("`http://127.0.0.1:8000/login.html`")
    page.locator('#username').fill('yoyo')
    page.locator('#password').fill('*********')

    with page.expect_response("*/api/login") as response_info:
        page.locator('#loginBtn').click()
    response = response_info.value
    print(response.status)
    print(response.headers)
    print(response.text())

运行结果

400
{'date': 'Fri, 22 Sep 2023 13:27:59 GMT', 'server': 'nginx/1.12.0', 'connection': 'keep-alive', 'content-length': '43', 'content-type': 'application/json'}
{"message": "用户名或密码不正确"}

标签:info,playwright,python,page,expect,print,response
From: https://www.cnblogs.com/yoyoketang/p/17723422.html

相关文章

  • Apache IoTDB开发系统之Python原生接口
    依赖在使用Python原生接口包前,您需要安装thrift(>=0.13)依赖。使用示例首先下载最新安装包:pip3installapache-iotdb注意:如果您想要安装0.13.0版本的PythonAPI,不要使用 pipinstallapache-iotdb==0.13.0,请使用 pipinstallapache-iotdb==0.13.0.post1 作为替代!您可......
  • # yyds干货盘点 # ChatGPT 实用小案例分享——使用Python重命名附件和统计发票合计金
    大家好,我是皮皮。一、前言前几天在【志军】的星球看到了一个有意思的ChatGPT分享,正好喝Python相关的,一起来看看吧。ChatGPT实用小案例分享。如果你在高德或者滴滴上申请过开票,应该知道它们会给我们发一封邮件,发票和行程单都会放在附件中。由于高德是聚合平台,背后有很多网约车平台,......
  • python+playwright 学习-82 Request 对象
    前言每当页面发送网络资源请求时,页面都会发出以下事件序列:page.on("request")当页面发出请求时触发page.on("response")接收到请求的响应状态和标头时触发page.on("requestfinished")当响应主体被下载并且请求完成时发出。如果请求在某个时刻失败,则会发出page.on("requ......
  • python的pandas库:合并数据
    在Pandas中,如果你有两个数据框(DataFrames),且它们的列数和列名都相同,你可以使用concat或merge函数将它们合并。以下是具体步骤:首先,导入Pandas库:importpandasaspd创建两个列数和列名都相同的数据框:df1=pd.DataFrame({'A':['A0','A1','A2','A3'],'B':[�......
  • Python如何获取GPS经纬度信息?
    需求:来了一个GPS设备:获取GPS经纬度信息解决:用serial库步骤:第0步:GPS协议介绍这里简单介绍xxRMC,是推荐最小定位信息。【红色,是要找的字段】$GPRMC 例:$GPRMC,024813.640,A,3158.4608,N,11848.3737,E,10.05,324.27,150706,,,A*50字段0:$GPRMC,语句ID,表明该语句为Recomm......
  • python 生成二维码 插入 excel
    生成二维码defmake_qrcode(data:str,path:str):qr=qrcode.QRCode(version=1,error_correction=qrcode.constants.ERROR_CORRECT_L,box_size=10,border=4)qr.add_data(data)qr.make(fit=True)img=qr.make_i......
  • nicegui:Python 图形界面库,简单好用
    #前言在现代计算机应用程序开发中,图形用户界面(GUI)是用户与程序交互的重要组成部分。然而,GUI开发往往需要大量的代码和复杂的布局,给开发者带来了一定的挑战。在本篇博文中,将介绍nicegui,它是一个简单易用的图形用户界面库,提供了一种简化GUI开发的方式,使开发者能够更快速地构......
  • Python-day15
    1、动态语言classanimal:defeat(self):print('animalwilleat')classdog(animal):defeat(self):print('dogiseatingbone')classcat(animal):defeat(self):print('catiseatingfish')class......
  • Python functools模块:提升函数式编程的5个常用函数
    Python的functools模块是标准库中的一个强大工具,提供了一系列函数,用于优化和增强函数式编程的能力。这些函数可以帮助我们处理函数、操作装饰器、缓存结果等。介绍functools模块中的五个常用函数,包括 partial、wraps、lru_cache、reduce和compose,并提供相关的代码示例,帮助更好......
  • Python中统计、拷贝等方法的使用
    一、统计方法的使用#coding=utf-8#统计出list中正数和负数的个数list=[1,2,3,-1,-2,-3]#count()函数--统计列表中某个元素出现的次数#print(list.count(2))#len()函数--统计列表长度即列表中的元素总个数#print(len(list))list1=[iforiinlistifi>0]print(l......