1 Star 0 Fork 0

jx0913/stock

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
foreignexchange.py 2.14 KB
一键复制 编辑 原始数据 按行查看 历史
Rocky Chen 提交于 2019-04-27 17:52 . update
# -*-coding=utf-8-*-
# @Time : 2018/8/7 13:45
# @File : foreignexchange.py
# 实时获取外汇
import os
import re
import datetime
import requests
from setting import sendmail,get_mysql_conn,llogger
filename=os.path.basename(__file__)
logger = llogger('log/'+filename)
class ForeighExchange(object):
def __init__(self):
self.url = 'http://data.bank.hexun.com/other/cms/foreignexchangejson.ashx?callback=ShowDatalist'
self.update_req = 10
self.retry = 5
self.headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'}
def run(self):
content = self.fetch_web()
if content:
pattern = re.compile('\{bank:\'工商银行\',currency:\'美元\',code:\'USD\',currencyUnit:\'\',cenPrice:\'\',(buyPrice1:\'[.0-9]+\',sellPrice1:\'[.0-9]+\'),.*?\'\}')
ret_str = pattern.search(content).group(1)
buy=re.search('buyPrice1:\'([0-9.]+)\'',ret_str).group(1)
sell=re.search('sellPrice1:\'([0-9.]+)\'',ret_str).group(1)
return (buy,sell)
def notice(self):
buy,sell=self.run()
sub = '{}: 美元汇率{}'.format(datetime.datetime.now().strftime('%Y-%m-%d %H:%M'),buy)
logger.info(sub)
# sendmail('',sub)
conn=get_mysql_conn('db_stock','local')
cursor = conn.cursor()
cmd = 'insert into `usd_ratio` (`price`,`date`) VALUES ({},{!r})'.format(buy,datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
try:
cursor.execute(cmd)
conn.commit()
except Exception as e:
logger.error(e)
conn.rollback()
conn.close()
def fetch_web(self):
for i in range(self.retry):
try:
r = requests.get(url=self.url,headers=self.headers)
if r.status_code==200:
return r.text
else:
continue
except Exception as e:
logger.error(e)
return None
logger.info('Start')
obj = ForeighExchange()
obj.notice()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jx0913/stock.git
git@gitee.com:jx0913/stock.git
jx0913
stock
stock
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385