python,在vs code中用jupyter notebook和playwright碰到的问题,包括同步异步、安装及运行等

python入门,碰到问题网上解决。

本文主要是提供在vs code中用jupyter notebook和playwright碰到的问题,包括同步异步、安装及运行等

错误类型

Error: It looks like you are using Playwright Sync API inside the asyncio loop. Please use the Async API instead.

RuntimeError: asyncio.run() cannot be called from a running event loop

参考文章

最新出炉 -Web自动化测试之playwright:概述_playwright codegen-CSDN博客,碰到一些问题。

用这篇文章主要是看到其可以找到开发者工具中网络中的内容。

Page对象提供一个on方法,用来监听页面中发送的各个事件,例如close、console、load、request、response等。

这里监听reponse事件,在每次网络请求得到响应的时候会出发这个事件,我们可以设置回调方法来获取响应中的全部信息,示例如下:

from playwright.sync_api import sync_playwright

def on_response(response):
    print(f'Statue {response.status}: {response.url}')

with sync_playwright() as p:
    browser = p.chromium.launch(headless=False)
    page = browser.new_page()
    page.on('response', on_response)
    page.goto('<https://spa6.scrape.center/>')
    page.wait_for_load_state('networkidle')
    browser.close()
1
2
3
4
5
6
7
8
9
10
11
12
创建Page对象后,开始监听response事件,同时将回调方法设置为on_response,on_response接收一个参数,然后输出响应中的状态码和链接。

运行上述代码后,可以看到控制台输出如下结果:

Statue 200: <https://spa6.scrape.center/>
Statue 200: <https://spa6.scrape.center/css/chunk-19c920f8.2a6496e0.css>
Statue 200: <https://spa6.scrape.center/css/app.ea9d802a.css>
Statue 200: <https://spa6.scrape.center/js/chunk-vendors.77daf991.js>
Statue 200: <https://spa6.scrape.center/js/app.5ef0d454.js>
Statue 200: <https://spa6.scrape.center/js/chunk-2f73b8f3.8f2fc3cd.js>
....
....
Statue 200: <https://p1.meituan.net/movie/6bea9af4524dfbd0b668eaa7e187c3df767253.jpg@464w_644h_1e_1c>
Statue 200: <https://p0.meituan.net/movie/289f98ceaa8a0ae737d3dc01cd05ab052213631.jpg@464w_644h_1e_1c>
Statue 200: <https://p0.meituan.net/movie/283292171619cdfd5b240c8fd093f1eb255670.jpg@464w_644h_1e_1c>
Statue 200: <https://p0.meituan.net/movie/223c3e186db3ab4ea3bb14508c709400427933.jpg@464w_644h_1e_1c>
Statue 200: <https://p1.meituan.net/movie/b607fba7513e7f15eab170aac1e1400d878112.jpg@464w_644h_1e_1c>
Statue 200: <https://p0.meituan.net/movie/8959888ee0c399b0fe53a714bc8a5a17460048.jpg@464w_644h_1e_1c>
Statue 200: <https://p0.meituan.net/movie/27b76fe6cf3903f3d74963f70786001e1438406.jpg@464w_644h_1e_1c>
————————————————

                            版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
                        
原文链接:https://blog.csdn.net/weixin_41905135/article/details/137168458

可以发现,这个输出结果其实正好对应浏览器Network面板中的所有请求和响应,和下图的内容一一对应。

3

这个网站,真实数据都是Ajax加载的,同时Ajax请求中还带有加密参数,不好轻易获取。但有了on_response方法,如果我们想截获Ajax请求,岂不是就非常容易了?

环境

windows11

visio studio code

playwright 1.46.0

python 3.12

安装

在参考文章中安装方法为

其中第二条命令,本人使用

pip -m playwright install

错误1 Error: It looks like you are using Playwright Sync API inside the asyncio loop. Please use the Async API instead.

jupyter note方式

参考文章中示例如下,同步。

首先要去掉第7行中“<>”符号。

在上述环境中运行,提示错误

Error: It looks like you are using Playwright Sync API inside the asyncio loop. Please use the Async API instead.

如下图。

网上查找了很多,都没有解决。

解决方法1 

本人解决办法是选择代码右键单击run code,可以。

输出如下,结果正常。只是乱码。

py格式

以.py文件在交互式窗口中运行也如此

解决方法2

同上,输出乱码,运行结果正常

解决方法3

在终端中运行,一切正常。

错误2 RuntimeError: asyncio.run() cannot be called from a running event loop

在运行参考文章异步示例时出现如上错误

解决方法4

同前。

.py格式也相同。

补充pycharm

在pycharm中两种运行方式都正常且没有乱码。

望大家提供根本原因及解决方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值