1 # -*- coding: utf-8 -*- 2 """ 3 獲取實時匯率 4 Created on Fri Oct 18 13:11:40 2013 5 6 @author: alala 7 """ 8 9 import httplib 10 import re 11 import MySQLdb 12 import datetime 13 14 URL = 'fx.cmbchina.com' #網站名 15 PATH = '/hq/' #頁面路徑 16 HOST = 'localhost' #數據庫地址(ip) 17 DB = "money" #數據庫名稱 18 USER = 'root' #數據庫用戶名 19 PSWD = 'sheet' #數據庫密碼 20 21 httpClient = None 22 23 try: 24 #抓去網頁內容 25 httpClient = httplib.HTTPConnection(URL, 80, timeout=30) 26 httpClient.request('GET', '/hq/') 27 response = httpClient.getresponse() 28 html = response.read() 29 #print html 30 31 #用正則表達式抓去匯率數據 32 reg = re.compile(r""" 33 <tr>\s*<td\s+class="fontbold">\s*(?P<name>\S+)\s*</td>\s* #交易幣 34 <td\s+align="center">\s*(?P<unit>\d+)\s*</td>\s* #交易幣單位 35 <td\s+align="center"\s+class="fontbold">\s*(?P<base>\S+)\s*</td>\s* #基本幣 36 <td\s*class="numberright">\s*(?P<midPrice>\d+\.\d+)\s*</td>\s* #中間價 37 <td\s*class="numberright">\s*(?P<sellPrice>\d+\.\d+)\s*</td>\s* #賣出價 38 <td\s*class="numberright">\s*(?P<buyPrice1>\d+\.\d+)\s*</td>\s* #現匯買入價 39 <td\s*class="numberright">\s*(?P<buyPrice2>\d+\.\d+)\s*</td>\s* #現鈔買入價 40 <td\s*align="center">\s*(?P<time>\d+:\d+:\d+)\s*</td>\s* #時間 41 """, re.MULTILINE | re.X) 42 rows = reg.findall(html) 43 #打印匯率數據 44 for r in rows: 45 print ','.join(map(str,r)), '\n' 46 47 #數據庫操做 48 #確保mysqldb已經安裝,能夠用下面的命令安裝 49 #pip install MySQL-python 50 51 #創建和數據庫系統的鏈接 52 conn = MySQLdb.connect(host=HOST, user=USER,passwd=PSWD) 53 54 #獲取操做遊標 55 cursor = conn.cursor() 56 #執行SQL,建立一個數據庫. 57 cursor.execute("CREATE DATABASE IF NOT EXISTS " + DB) 58 59 #選擇數據庫 60 conn.select_db(DB); 61 #執行SQL,建立一個數據表. 62 cursor.execute("""CREATE TABLE IF NOT EXISTS exchange_rate( 63 name VARCHAR(50) COMMENT '交易幣' PRIMARY KEY, 64 unit INT COMMENT '交易幣單位', 65 base VARCHAR(50) COMMENT '基本幣', 66 midPrice FLOAT COMMENT '中間價', 67 sellPrice FLOAT COMMENT '賣出價', 68 buyPrice1 FLOAT COMMENT '現匯買入價', 69 buyPrice2 FLOAT COMMENT '現鈔買入價', 70 time DATETIME COMMENT '時間' ) """) 71 records = [] 72 for r in rows: 73 (name,unit,base,midPrice,sellPrice,buyPrice1,buyPrice2,time) = r 74 time = datetime.datetime.strptime(datetime.datetime.now().strftime('%Y-%m-%d') 75 + " " + time,'%Y-%m-%d %H:%M:%S') 76 record = (name,int(unit),base,float(midPrice),float(sellPrice), 77 float(buyPrice1),float(buyPrice2),time) 78 records.append(record) 79 #print records 80 #更新匯率 81 cursor.executemany("REPLACE exchange_rate VALUES(%s,%s,%s,%s,%s,%s,%s,%s)" 82 ,records); 83 conn.commit() 84 85 #關閉鏈接,釋放資源 86 cursor.close(); 87 88 except Exception,e: 89 print e 90 finally: 91 if httpClient: 92 httpClient.close()