爬虫

主体

import requests
import time
import re
import os
import platform

import json
import paho.mqtt.client as mqtt

import price

debug = 0


class LocalDataClass:
    class PL:
        class BC:
            class HB:
                BTC = 1.67044451
            class OK:
                BTC = 1.3308
                ETH = 5
            SUM_BTC = 0
            SUM_CNY = 0
        class BK:
            ZH = 0
            ZFB = 0
            HYQ = 0
            SUM = 0
        SUM = 0
    class WEB:
        BTC_USDT = 0
        BTC_CNY = 0
        BTC_Amount = 0
        ETH_USDT = 0
        ETH_CNY = 0
        ETH_Amount = 0
        USDT_CNY = 6.4909
    def GetSum(self):
        if price.GetPrice(self) == 0:
            self.PL.BC.SUM_BTC = round((self.PL.BC.HB.BTC + self.PL.BC.OK.BTC), 8)
            self.PL.BC.SUM_CNY = int(self.PL.BC.SUM_BTC * self.WEB.BTC_CNY) + int(self.PL.BC.OK.ETH * self.WEB.ETH_CNY)
            self.PL.BK.SUM = self.PL.BK.ZH + self.PL.BK.ZFB + self.PL.BK.HYQ
            self.PL.SUM = int(self.PL.BK.SUM + self.PL.BC.SUM_CNY)
        else:
            print('get url err return')
    def Show(self,temp):
        if debug == 0:
            if platform.system().lower() == 'windows':
                os.system("cls")
            elif platform.system().lower() == 'linux':
                os.system("clear")
        if temp == 1:
            print('+++++++++++++++++++SHOW+++++++++++++++++++')
        else:
            print('-------------------SHOW-------------------')
        print('TEMP:')
        print('\tUSDT/CNY:',self.WEB.USDT_CNY)
        print('\tBTC:')
        print('\t\tUSDT:',self.WEB.BTC_USDT,'\t',format(self.WEB.BTC_Amount,".2f") +'%')
        print('\t\tCNY:',self.WEB.BTC_CNY)
        print('\tETH:')
        print('\t\tUSDT:',self.WEB.ETH_USDT,'\t',format(self.WEB.ETH_Amount,".2f") +'%')
        print('\t\tCNY:',self.WEB.ETH_CNY,'\t')
        print('PL:')
        print('\tBTC:',self.PL.BC.SUM_BTC)
        print('\tETH:',self.PL.BC.OK.ETH)
        print('\tSUM:',self.PL.BC.SUM_CNY)
        print('BK:')
        print('\tZH:',self.PL.BK.ZH)
        print('\tZFB:',self.PL.BK.ZFB)
        print('\tHYQ:',self.PL.BK.HYQ)
        print('\tSUM:',self.PL.BK.SUM)
        print('SUM:',self.PL.SUM)
        if temp == 1:
            print('+++++++++++++++++++SHOW+++++++++++++++++++')
        else:
            print('-------------------SHOW-------------------')
    def GetJson(self):
        data = {
            'Temp':{
                'BTC':{
                    'USDT':self.WEB.BTC_USDT,
                    'CNY':self.WEB.BTC_CNY,
                    'Amount':format(self.WEB.BTC_Amount,".2f") +'%'
                },
                'ETH':{
                    'USDT':self.WEB.ETH_USDT,
                    'CNY':self.WEB.ETH_CNY,
                    'Amount':format(self.WEB.ETH_Amount,".2f") +'%'
                },
            },
            'PL':{
                'BTC':self.PL.BC.SUM_BTC,
                'ETH':self.PL.BC.OK.ETH,
                'SUM':self.PL.BC.SUM_CNY,
            },
            'BK':{
                'ZH':self.PL.BK.ZH,
                'ZFB':self.PL.BK.ZFB,
                'HYQ':self.PL.BK.HYQ,
                'SUM':self.PL.BK.SUM,
            },
            'SUM':self.PL.SUM
        }
        data2 = json.dumps(data, indent=4)
        #print(data2)
        #MqttSend(data2)

#client = mqtt.Client()
def on_connect(client, userdata, flags, rc):
    print("Connected with result code: " + str(rc))
def on_message(client, userdata, msg):
    #print(msg.topic + " " + str(msg.payload))
    #print(msg.topic + " " + msg.payload)
    print(msg.topic,':',json.loads(msg.payload))
def MqttInit():
    client.on_connect = on_connect
    client.on_message = on_message
    client.connect('broker.emqx.io', 1883, 600) # 600为keepalive的时间间隔
    client.subscribe('xixi', qos=0)
    client.loop_start()
def MqttSend(data):
    client.publish('xixi', payload=data, qos=0)

#从带逗号的字符串恢复成数字   
def restoreNumber(numStr):
    pattern=re.compile('\D')
    numList=pattern.split(numStr)
    numStr=''.join(numList)
    return float(numStr)/100

#client = mqtt.Client()
def main():
    temp = 0
    #MqttInit()
    LocalData = LocalDataClass()    #创建对象
    while True:
        LocalData.GetSum()              #爬取数据
        #price.GetPrice(LocalData)
        #price.echo('hello qiqi xix')
        LocalData.Show(temp)            #显示数据
        #price.GetUsdtCny(LocalData)          #爬取汇率
        #LocalData.GetJson()             #转换为JSON格式
        temp = temp%2 +1
        time.sleep(1)
main()

爬取

import requests
import time
import re
import os
import platform

import json
import paho.mqtt.client as mqtt

def echo(self):
    print(self)
def GetPrice(self):
    try:
        response = requests.get('https://price.btcfans.com/exchange.huobi/',timeout=3)
        print('status:',response.status_code)
        if response.status_code == 200:
            print('test 1')
            tempH = re.findall(r'<span class="base">(.*?)</span>',response.text)
            #temp = re.findall(r'<div class="main">(.*?)</div>',response.text)
            tempL = re.findall(r'<div class="table-col col2" data-last="(.*?)">',response.text)
            tempM = re.findall(r'<div data-changeper="(.*?)" class',response.text)
            print('test 2')
            for i in range(0,10):
                if tempH[i] == 'BTC':
                    self.WEB.BTC_USDT = float(tempL[i])
                    self.WEB.BTC_Amount = float(tempM[i])
                if tempH[i] == 'ETH':
                    self.WEB.ETH_USDT = float(tempL[i])
                    self.WEB.ETH_Amount = float(tempM[i])
            print(self.WEB.BTC_Amount,self.WEB.ETH_Amount)
            print(self.WEB.BTC_USDT,self.WEB.ETH_USDT,self.WEB.USDT_CNY)
            self.WEB.BTC_CNY = round(self.WEB.BTC_USDT * self.WEB.USDT_CNY,2)
            self.WEB.ETH_CNY = round(self.WEB.ETH_USDT * self.WEB.USDT_CNY,2)
            return 0
    #except requests.exceptions.RequestException:
    except BaseException:
        print ("\nget url error !")
        return  1

def GetUsdtCny(self):
    try:
        kv = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36',
  'referer': 'https://www.mzitu.com/',
  'cookie': 'Hm_lvt_dbc355aef238b6c32b43eacbbf161c3c=1575636408; Hm_lpvt_dbc355aef238b6c32b43eacbbf161c3c=1575636892'}
        #response = requests.get('https://www.usd-cny.com/usd-rmb.htm',timeout=3,headers = kv)
        response = requests.get('https://www.usd-cny.com/usd-rmb.htm',timeout=5)
        if response.status_code == 200:
            temp = re.findall(r'<span class="up">(.*?)</span>',response.text)
            #self.WEB.USDT_CNY =
            print('xixi1')
            print('xixi2',temp)
            print('xixi3',response.text)
            print('xixi3',temp[1])
            print('xixi4',temp[2])
            data = round(float(temp[0]) / 10000,4)
            if data > 0:
                self.WEB.USDT_CNY = data
    except BaseException:
        print ("\nget USDT url error ! err:",response.status_code)
        return  1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值