forked from Rockyzsu/stock
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathforeignexchange.py
67 lines (52 loc) · 2.09 KB
/
foreignexchange.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# -*-coding=utf-8-*-
# @Time : 2018/8/7 13:45
# @File : foreignexchange.py
# 实时获取外汇
import re
import datetime
import requests
from setting import sendmail,get_mysql_conn,llogger
logger = llogger(__file__)
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()