前言

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": "用户名或密码不正确"}

文章转自:https://www.cnblogs.com/yoyoketang/p/17723422.html