如何在 websocket 中解码此消息

问题描述 投票:0回答:3

我的代码:

from selenium import webdriver
import sys
import  time
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.chrome.options import Options
import os
import json

chrm_options=Options()
chrm_caps = webdriver.DesiredCapabilities.CHROME.copy()
chrm_caps['goog:loggingPrefs'] = { 'performance':'ALL' }
#driver = webdriver.Chrome(executable_path = 'chromedriver.exe', chrome_options=chrm_options,desired_capabilities=chrm_caps) Windows
driver = webdriver.Chrome(executable_path = 'C:\chromedriver.exe', chrome_options=chrm_options,desired_capabilities=chrm_caps) #Linux


def LoadWebDriver() :
    print("Web driver Init ")
    base_url = "https://pocketoption.com/en/cabinet/demo-quick-high-low/#td"  #Change the site
    print('Loading URL ....')
    driver.get(base_url )
    

def WebSocketLog():
    for wsData in driver.get_log('performance'):
        #print(wsData) 
        wsJson = json.loads((wsData['message']))
        print(wsData)
#         if wsJson["message"]["method"]== "Network.webSocketFrameReceived":
#             print ("Rx :"+ str(wsJson["message"]["params"]["timestamp"]) + wsJson["message"]["params"]["response"]["payloadData"])
#         if wsJson["message"]["method"] =="Network.webSocketFrameSent":
#             print ("Tx :"+ wsJson["message"]["params"]["response"]["payloadData"])


LoadWebDriver()
print("Waiting")
time.sleep(30) #Capture messages for 30sec, integrate your code here

已收到消息:

Rx :451-["updateStream",{"_placeholder":true,"num":0}]
Rx :W1siQVVEQ0hGIiwxNjMzNTI1MDg2LjM2NiwwLjY3Mjc4XV0=
Rx :451-["updateStream",{"_placeholder":true,"num":0}]
Rx :W1siQVVEQ0hGIiwxNjMzNTI1MDg2LjU0NywwLjY3MjgxXV0=
Rx :451-["updateStream",{"_placeholder":true,"num":0}]
Rx :W1siQVVEQ0hGIiwxNjMzNTI1MDg2LjY5NywwLjY3Mjc5XV0=
Rx :451-["updateStream",{"_placeholder":true,"num":0}]
Rx :W1siQVVEQ0hGIiwxNjMzNTI1MDg3LjU2NiwwLjY3Mjc5XV0=

在 pupter 消息中是上帝,但在 selenium 中却是这样的

python selenium selenium-webdriver
3个回答
2
投票

您好,Rx 值看起来像 Base64 编码的字符串。使用下面的函数后,我得到了这个值,这是你所期望的吗?

[["AUDCHF",1633525087.566,0.67279]]

import base64    
def base64ToString(b):
        return base64.b64decode(b).decode('utf-8')
        
b = "W1siQVVEQ0hGIiwxNjMzNTI1MDg3LjU2NiwwLjY3Mjc5XV0="
print(base64ToString(b))

0
投票

selenium4 的更新版本:

import base64
import json
import time

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.set_capability('goog:loggingPrefs', {'performance': 'ALL'})
driver = webdriver.Chrome(options=options)


def LoadWebDriver():
    base_url = "https://pocketoption.com/en/cabinet/demo-quick-high-low/"  # Change the site
    driver.get(base_url)


def WebSocketLog():
    for wsData in driver.get_log('performance'):
        message = json.loads(wsData['message'])['message']
        response = message.get('params', {}).get('response', {})
        if response.get('opcode', 0) == 2:
            payload_str = base64.b64decode(response['payloadData']).decode('utf-8')
            try:
                symbol, timestamp, value = json.loads(payload_str)[0]
            except:
                return
            print(symbol, timestamp, value)


LoadWebDriver()
time.sleep(60)  # delay for typing login and password
while True:
    WebSocketLog()

-2
投票

我们如何通过Python中的selenium在https://pocketoption.com/en/cabinet/demo-quick-high-low(金额框)中发送交易金额?

© www.soinside.com 2019 - 2024. All rights reserved.