DrissionPage/docs/使用方法/监听浏览器网络数据.md
2022-05-20 13:08:15 +00:00

61 lines
1.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 监听浏览器网络数据
许多网页的数据来自接口,在网站使用过程中动态加载,如使用 JS 加载内容的翻页列表。
这些数据通常以 json 形式发送,浏览器接收后,对其进行解析,再加载到 DOM 相应位置。
以前,我们从 DOM 中去获取解析后数据的,可能存在
## 控制浏览器同时监听请求
复制此代码可直接运行查看结果。
```python
'''
如果监听特定请求的Chrome浏览器数据可以采用如下代码
'''
p = MixPage(driver_options=do, session_options=False)
#监听浏览器
l =Listener(p)
# 找到需要监听的链接
l.listen('JobSearchResult.aspx',count=10,asyn=True)
p.ele(".next-page PageNumber").click(by_js=True)
for i in l.steps():
scode = i[0].body
# 一旦数据出来,立即停止加载
p.stop_loading()
#使用解析网页
p.ele(".next-page PageNumber").click(by_js=True)
#停止监听,可以设置条件
l.stop()
```
## 监听指定端口的请求
复制此代码可直接运行查看结果。
```python
'''
如果仅仅是监听Chrome浏览器的请求可以采用下面的代码
'''
from FlowViewer import Listener
#监听有端口的浏览器
l =Listener()
l.listen(None,count=None,asyn=True)
for i in l.steps():
print(i[0].url)
print(i[0].body)
#停止监听,可以设置条件或者跳出循环
l.stop()
```