代码拉取完成,页面将自动刷新
# -*-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()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。