美文网首页
比特币价格爬虫(高于某个价格自动发邮件)

比特币价格爬虫(高于某个价格自动发邮件)

作者: 曾柏超 | 来源:发表于2018-01-14 21:47 被阅读0次
#coding=utf-8
import os
import re
import time
import requests
import json
import time
from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import smtplib

def _format_addr(s):
    name, addr = parseaddr(s)
    return formataddr((Header(name, 'utf-8').encode(), addr))

money = 0

from_addr = 'zhengbaich@163.com'
password = 'pwd'
to_addr = '565618435@qq.com'
smtp_server = 'smtp.163.com'

msg = MIMEText('hello, send by Python...', 'plain', 'utf-8')
msg['From'] = _format_addr('bitcoin <%s>' % from_addr)
msg['To'] = _format_addr('管理员 <%s>' % to_addr)
msg['Subject'] = Header('现在价格已经高于104,388', 'utf-8').encode()

print ("----------time-------|--------price----------")

while (money < 104388):
    header = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'}

    url = 'https://api-otc.huobi.pro/v1/otc/trade/list/public?coinId=1&tradeType=0&currentPage=1&payWay=&country=&merchant=1&online=1&range=0'
    r = requests.get(url, headers=header)
    text = r.text
    s = json.loads(text)
    price = s['data'][0]['price']

    # 格式化成2016-03-20 11:45:39形式
    print time.strftime("%Y-%m-%d %H:%M:%S  | ", time.localtime()) ,price
    # print(price)
    print ("-------------------------------------------|")




    money = price
    time.sleep(20)

print "Good bye!"

try:
    server = smtplib.SMTP(smtp_server, 25)
    server.login(from_addr, password)
    server.sendmail(from_addr, [to_addr], msg.as_string())
    server.quit()
    print 'send OK'
except Exception, e:
    print str(e)


#买入BTC
# https://api-otc.huobi.pro/v1/otc/trade/list/public?coinId=1&tradeType=1&currentPage=1&payWay=&country=&merchant=1&online=1&range=0




相关文章

网友评论

      本文标题:比特币价格爬虫(高于某个价格自动发邮件)

      本文链接:https://www.haomeiwen.com/subject/vxploxtx.html