python之websocket数据爬取

**

python之websocket数据爬取

**

首先我爬取的是 货币网的 https://www.zbg.com/exOtc
python之websocket数据爬取_第1张图片
爬取商家的购买和出售的数据
下面是我利用websocket库进行爬取的代码
不废话,直接上码.

import json, time
from websocket import create_connection


class OTC:
    #number 是区分购买和出售
    number = 0
    #这个 url 是websocket 长连接的url
    url = 'wss://www.zbg.com/exchange/base/socket/WebSocketHandler'
    #定义好请求的数据
    exc_symbol = {
        'currencyName': 'usdt',
        'pageIndex': 1,
        'pageSize': 10,
        'salesTerritory': 'cny',
        'subscribe_event': 'trade',
        'tradeType': 0,
    }
    exc_symbol_2 = {
        'currencyName': 'usdt',
        'pageIndex': 1,
        'pageSize': 10,
        'salesTerritory': 'cny',
        'subscribe_event': 'trade',
        'tradeType': 1,
    }

    def get_web_socket(self):
        while 1:
            if self.number == 0:
                try:
                    # 链接服务器
                    ws = create_connection(self.url)
                except Exception as e:
                    print('retry to connect...')
                    continue
                # 发送消息,把请求的数据类似于post表单的数据发送给服务器获取想要的数据
                ws.send(json.dumps(self.exc_symbol))
                # 接收消息,然后把数据处理一下打印!
                data = ws.recv()
                result = json.loads(data)
                tradeList = result['datas']['tradeList']
                spiderdata = []
                for trade in tradeList:
                    info_data = {}
                    info_data['商家'] = (trade['userName'])  # 商家信息
                    info_data['订单量'] = (trade['lastOrderCount'])  # 订单量
                    number = trade['amount'][0:6]
                    info_data['数量'] = number  # 数量
                    fixedPrice = trade['fixedPrice'][0:6]
                    info_data['单价'] = fixedPrice  # 单价
                    spiderdata.append(info_data)
                print(spiderdata)
                print("-" * 200)
                self.number += 1
                # 数据监测间隔
                time.sleep(1)
            # 这里是爬取出售的数据
            if self.number == 1:
                try:
                    ws = create_connection(self.url)
                except Exception as e:
                    print('retry to connect...')
                    continue
                # 发送消息
                ws.send(json.dumps(self.exc_symbol_2))
                # 接收消息
                data = ws.recv()
                result = json.loads(data)
                tradeList = result['datas']['tradeList']
                spiderdata = []
                for trade in tradeList:
                    info_data = {}
                    info_data['商家'] = (trade['userName'])  # 商家信息
                    info_data['订单量'] = (trade['lastOrderCount'])  # 订单量
                    number = trade['amount'][0:6]
                    info_data['数量'] = number  # 数量
                    fixedPrice = trade['fixedPrice'][0:6]
                    info_data['单价'] = fixedPrice  # 单价
                    spiderdata.append(info_data)
                print(spiderdata)
                print("-" * 200)
                self.number += 1
                # 数据监测间隔
                time.sleep(1)
            if self.number == 2:
                break

otc = OTC()
otc.get_web_socket()

python之websocket数据爬取_第2张图片
这是我爬取的结果
谢谢!

你可能感兴趣的:(python之websocket数据爬取)