前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python+playwright 学习-83 page.expect_response()捕获网络返回数据

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

作者头像
上海-悠悠
发布2023-09-24 20:57:32
4960
发布2023-09-24 20:57:32
举报

前言

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

expect_response() 使用

官方文档示例

代码语言:javascript
复制
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 lambdawith 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()方法更改默认值。

使用示例

示例代码

代码语言:javascript
复制
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())

运行结果

代码语言:javascript
复制
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": "用户名或密码不正确"}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2023-09-23 08:19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 从零开始学自动化测试 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • expect_response() 使用
  • 使用示例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档