husonet | Tarih: 23.05.2014
TCMB Üzerinden Kurlarin İndirilmesi
Aşağıdaki betik TCMB üzerinden kurları indirip parse eder
#!/usr/bin/python
# -*- coding:utf-8 -*-
################################################################################
# Huseyin OZDEMIR
# husonet
# 23.05.2014
# Bu betik TCMB uzerinden kurlari indirmesi icin yazilmistir
################################################################################
import sys
import time
import re
import urllib
RE_DESEN = '<Currency.*?CurrencyCode="(.*?)">.*?<ForexBuying>(.*?)</ForexBuying>'
RE_DESEN+= '<ForexSelling>(.*?)</ForexSelling>'
RE_DESEN+= '<BanknoteBuying>(.*?)</BanknoteBuying>'
RE_DESEN+= '<BanknoteSelling>(.*?)</BanknoteSelling>.*?</Currency>'
RE_DESEN = re.compile(RE_DESEN, re.DOTALL)
# DEBUG etmek icin 1
DEBUG = 1
#-------------------------------------------------------------------------------
# tcbmm sayfasindan kurlari getirir
def get_kur():
try:
result = ''
p = urllib.urlopen('http://www.tcmb.gov.tr/kurlar/today.xml')
result = p.read()
except Exception, err:
if DEBUG:
raise
else:
print(str(err))
result = None
finally:
return result
#-------------------------------------------------------------------------------
# kurlari ayatlayan fonksiyon
def get_parse_kur(sBody):
try:
result = ''
g = RE_DESEN.findall(sBody)
result = g
except Exception, err:
if DEBUG:
raise
else:
print(str(err))
result = None
return result
#-------------------------------------------------------------------------------
# kurlari parse eder
def kur_islemleri():
try:
body = get_kur()
parses = get_parse_kur(body)
for kod, alis, satis, efektif_alis, efektif_satis in parses:
print kod
print alis
print satis
print efektif_alis
print efektif_satis
except Exception, err:
if DEBUG:
raise
else:
print(str(err))
result = None
if __name__ == '__main__':
try:
kur_islemleri()
except Exception, err:
print(str(err))