前言
page.goto()可以通过浏览器直接发get请求, playwright也可以支持通过浏览器发送post请求。
page.goto()使用
page.goto() 访问网站的时候,实际上是有返回值的,可以获取到response 对象
代码语言:javascript
复制
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()) # 响应正文</code></pre></div></div><h2 id="4up1c" name="playwright-%E5%8F%91post%E8%AF%B7%E6%B1%82"><strong>playwright 发post请求</strong></h2><p>接下来发一个post请求</p><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>javascript</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-javascript"><code class="language-javascript" style="margin-left:0">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())</code></pre></div></div><p>除了post请求,page.request 还可以支持其他请求方式