通过WebSocket抓取数据

时间:2020-06-16 13:12:20

标签: python web-scraping encoding websocket scrapy

我正在尝试从此特定网页获取每日价格数据:

https://www.londonstockexchange.com/stock/CS1/amundi/company-page

这些数据在图表中表示。

我想尽办法去获取这些数据。我假设这些数据是通过在浏览器控制台中建立并可检索的一个websocket连接进行传输的。

enter image description here

我试图模拟websocket连接,并发送与前端应用相同的二进制文件。

from websocket import create_connection

s = create_connection("wss://82-99-29-151.infrontservices.com/wsrt/2/4")

hex_1 = "3e000000010..."
hex_2 = "13000000010..."
hex_3 = "1e000000010..."

ws.send(binascii.unhexlify(hex_1))
ws.send(binascii.unhexlify(hex_2))
ws.send(binascii.unhexlify(hex_3))

result =  ws.recv()

然后,我尝试使用以下所有可能的编码来解码此响应:

import binascii
from encodings.aliases import aliases

for v in [v for k, v in aliases.items()]:
    try:
        print(result.decode(v))
    except:
        print(f"ERROR {v}")

自然,我没有可以利用的可解释输出。我可以认为这里使用的是密码。但是我不知道如何进一步调查。

您对此有任何想法吗? :)

提前谢谢!

AL Ko

修改1

enter image description here

对于给定日期,我们可以看到一个值为16990的数据点。这就是我想要的图表的整个时间序列。

1 个答案:

答案 0 :(得分:0)

在阅读我的评论并获得有关刮擦的信息后,决定谨慎进行

Python只需几行代码即可检索此JSON

import requests
url = "https://api.londonstockexchange.com/api/gw/lse/instruments/alldata/CS1"
response = requests.get(url=url).json()
# print some data from the json
print(response_json)
print(response_json.get("description"))
print(response_json.get("bid"))

我使用“网络”标签找到了这些数据,当您点击“重新加载”时,还会显示更多数据,但是它们似乎为空。