前言

page.goto()可以通过浏览器直接发get请求, playwright也可以支持通过浏览器发送post请求。

page.goto()使用

page.goto() 访问网站的时候,实际上是有返回值的,可以获取到response 对象

from playwright.sync_api import sync_playwright, expect

# 上海悠悠 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()

    resp = page.goto('https://www.cnblogs.com/yoyoketang/')
    print(resp.status)    # 状态码
    print(resp.headers)   # 头部
    print(resp.body())    # 响应正文

playwright 发post请求

接下来发一个post请求

from playwright.sync_api import sync_playwright, expect

with sync_playwright() as p:
    browser = p.chromium.launch(headless=False)
    context = browser.new_context()
    page = context.new_page()

    page.goto('https://www.cnblogs.com/yoyoketang/')


    # 发post请求
    resp = page.request.post(
        url="http://www.example.com/",
        data={"user": "test", "email": "123@qq.com"}
    )
    print(resp.status)
    print(resp.headers)
    print(resp.body())

除了post请求,page.request 还可以支持其他请求方式

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