playwright 跳转网页 python
时间: 2023-06-18 07:02:52 浏览: 165
使用Playwright库在Python中跳转网页的操作步骤如下:
1. 首先安装Playwright库,可以使用以下命令进行安装:
```
pip install playwright
```
2. 导入Playwright库:
```python
from playwright.sync_api import Playwright, sync_playwright
```
3. 启动一个浏览器实例:
```python
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
```
这里以Chromium浏览器为例,如果要使用其他浏览器,可以将`p.chromium`改为`p.firefox`或`p.webkit`。
4. 跳转到指定网页:
```python
page.goto('https://www.example.com')
```
这里以`https://www.example.com`为例,可以根据需求修改为其他网页。
完整示例代码如下:
```python
from playwright.sync_api import Playwright, sync_playwright
with sync_playwright() as p:
# 启动Chromium浏览器
browser = p.chromium.launch()
# 创建一个新页面
page = browser.new_page()
# 跳转到指定网页
page.goto('https://www.example.com')
# 关闭浏览器
browser.close()
```
阅读全文