websocket使用

一般用于股票等类型网站

http协议

image-20230326193355815

image-20230326193318193

import requests

# http协议.....
url = "http://43.push2.eastmoney.com/api/qt/stock/trends2/sse?fields1=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f17&fields2=f51,f52,f53,f54,f55,f56,f57,f58&mpi=1000&ut=fa5fd1943c7b386f172d6893dbfba10b&secid=1.603896&ndays=1&iscr=0&iscca=0&wbp2u=|0|0|0|web"

headers = {
    "Referer": "http://q![image-20230326193202339](https://img2023.cnblogs.com/other/2867340/202303/2867340-20230326193909691-38103348.png)uote.eastmoney.com/",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
}

# 对于服务器返回的是流的时候.可以用stream参数
resp = requests.get(url, headers=headers, stream=True)

data = resp.iter_lines().__next__()
print(data)

websocket协议

image-20230326192252332

image-20230326192150161

# websocket
# 1. ws或wss协议
# 2. 状态码是101


# pip install websockets
# 我们只需要一个可以发送内容以及接受内容的地方
import websockets
import asyncio


async def send(ws):
    await ws.send("")


async def recv(ws):
    await ws.recv()


async def main():
    url = "wss://hq.sinajs.cn/wskt?list=sz399006"
    # url = "wss://hq.sinajs.cn/wskt?list=bj430047,bj430047_i"
    # extra_headers给ws 传递请求头.
    async with websockets.connect(url, extra_headers={
        "Origin": "https://finance.sina.com.cn",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
    }) as ws:
        # 1. 接一个发一个.  单线程....
        # while 1:
        #     await ws.send("")
        #     ret = await ws.recv()
        #     print(ret)
        # 2. 不定时的发送. 不定时的接收.
        # t1 = asyncio.create_task(send(ws))
        # t2 = asyncio.create_task(recv(ws))
        # await asyncio.wait([t1, t2])

        # 本案例中. 只需要接受一次即可.
        msg = await ws.recv()
        print(msg)
        print(ws)


if __name__ == '__main__':
    asyncio.run(main())
posted @ 2023-03-26 19:40  凫弥  阅读(229)  评论(0编辑  收藏  举报