Python Forum

Full Version: web scrap currency
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
good day
please help me
i wanna get some data about currency rates from our local site "http://nationalbank.kz/?furl=cursFull&switch=rus".
and can't get data under "td" tag such as: currency name, currency ratio, currency costs.
i can't filter it by class or other tag.
can anyone tell how extract that data preferably separately by fields?
We are glad to help, but noone will write code for you.
Post what you have tried in Python code tags (you can find help here). Also include the actual result you get vs. desired result.
nobody must write for me. that is my task.
maybe someone can give a direction?
i tried to use lambda function, class2_2 = class2[0].get_text() and simple cycle like for ... in ... : print

part of actual result output:
<td align="center" class="gen7"></td>
<td align="left" class="gen7">

1 ДОЛЛАР США

</td>
<td align="center" class="gen7">USD / KZT</td>
<td align="center" class="gen7">330.78</td>
<td align="left" class="gen7" valign="middle" width="10">
</td>
<td align="center" class="gen7"></td>
<td align="left" class="gen7">

1 ЕВРО

</td>
<td align="center" class="gen7">EUR / KZT</td>
<td align="center" class="gen7">387.31</td>
<td align="left" class="gen7" valign="middle" width="10">
</td>

desired result:
1 ДОЛЛАР США USD / KZT 330.78
(Jun-04-2018, 08:07 AM)ikeen Wrote: [ -> ]nobody must write for me. that is my task.
maybe someone can give a direction?
i tried to use lambda function, class2_2 = class2[0].get_text() and simple cycle like for ... in ... : print

as @j.crater said - we need to see your code in python tags.
from urllib.request import urlopen #as url
import pprint
from bs4 import BeautifulSoup, Comment #as bsobj
from xml.etree import ElementTree as ET
import string as s
import pandas
 
idValI = (5, 6)
id_cur = idValI
dollar = "1 ДОЛЛАР США"
id_dollar = '5'
evro = "1 ЕВРО"
id_evro = '6'

def getTitle(url):
    try:
        html = urlopen(url)
    except HTTPError as e:
        print (e)
        return None
    try:
        obj = BeautifulSoup(html.read())
        title = obj.body.h1
    except AttributeError as e:
        return None
    return title

html = urlopen("http://nationalbank.kz/?furl=cursFull&switch=rus")
bsobj = BeautifulSoup(html,"html.parser")

class1 = bsobj.select("div h1")
doc = class1[0].get_text() ## date of courses
print (doc.replace('\t', ''))
#toc =  table of courses
 
cur_search = {'dollar', 'evro'}
class2 = bsobj.find_all("td", {"class":"gen7"})
result = {}
#class2_2 = class2[0].get_text()
for toc in class2:
    if 'USD / KZT' in class2: print (toc)
    elif class2 == 'EUR / KZT': print (toc)
    print (toc)
import requests
from bs4 import BeautifulSoup
url = 'http://nationalbank.kz/?furl=cursFull&switch=rus'

resp = requests.get(url)
soup = BeautifulSoup(resp.text, 'html.parser')
tbl = soup.find('table', {'class':'gen4'})
for tr in tbl.find_all('tr'):
    my_data = tr.find_all('td', {'class':'gen7'})
    print(','.join(td.text.strip() for td in my_data))
Output:
1 АВСТРАЛИЙСКИЙ ДОЛЛАР,AUD / KZT,249.54,, 1 АЗЕРБАЙДЖАНСКИЙ МАНАТ,AZN / KZT,195.38,, 10 АРМЯНСКИЙ ДРАМ,AMD / KZT,6.86,, 1 БЕЛОРУССКИЙ РУБЛЬ,BYN / KZT,165.14,, 1 БРАЗИЛЬСКИЙ РЕАЛ,BRL / KZT,88.86,, 10 ВЕНГЕРСКИХ ФОРИНТОВ,HUF / KZT,12.12,, 1 ГОНКОНГСКИЙ ДОЛЛАР,HKD / KZT,42.16,, 1 ГРУЗИНСКИЙ ЛАРИ,GEL / KZT,133.92,, 1 ДАТСКАЯ КРОНА,DKK / KZT,52.05,, 1 ДИРХАМ ОАЭ,AED / KZT,90.06,, 1 ДОЛЛАР США,USD / KZT,330.78,, 1 ЕВРО,EUR / KZT,387.31,, 1 ИНДИЙСКАЯ РУПИЯ,INR / KZT,4.93,, 1000 ИРАНСКИЙ РИАЛ,IRR / KZT,7.85,, 1 КАНАДСКИЙ ДОЛЛАР,CAD / KZT,255.63,, 1 КИТАЙСКИЙ ЮАНЬ,CNY / KZT,51.56,, 1 КУВЕЙТСКИЙ ДИНАР,KWD / KZT,1094.94,, 1 КЫРГЫЗСКИЙ СОМ,KGS / KZT,4.84,, 1 МАЛАЗИЙСКИЙ РИНГГИТ,MYR / KZT,83.17,, 1 МЕКСИКАНСКИЙ ПЕСО,MXN / KZT,16.64,, 1 МОЛДАВСКИЙ ЛЕЙ,MDL / KZT,19.71,, 1 НОРВЕЖСКАЯ КРОНА,NOK / KZT,40.64,, 1 ПОЛЬСКИЙ ЗЛОТЫЙ,PLN / KZT,89.8,, 1 РИЯЛ САУДОВСКОЙ АРАВИИ,SAR / KZT,88.2,, 1 РОССИЙСКИЙ РУБЛЬ,RUB / KZT,5.32,, 1 СДР,XDR / KZT,468.62,, 1 СИНГАПУРСКИЙ ДОЛЛАР,SGD / KZT,247.37,, 1 ТАДЖИКСКИЙ СОМОНИ,TJS / KZT,36.7,, 1 ТАЙСКИЙ БАТ,THB / KZT,10.34,, 1 ТУРЕЦКАЯ ЛИРА,TRY / KZT,71.8,, 100 УЗБЕКСКИХ СУМОВ,UZS / KZT,4.14,, 1 УКРАИНСКАЯ ГРИВНА,UAH / KZT,12.67,, 1 ФУНТ СТЕРЛИНГОВ СОЕДИНЕННОГО КОРОЛЕВСТВА,GBP / KZT,440.24,, 1 ЧЕШСКАЯ КРОНА,CZK / KZT,15.01,, 1 ШВЕДСКАЯ КРОНА,SEK / KZT,37.68,, 1 ШВЕЙЦАРСКИЙ ФРАНК,CHF / KZT,335.99,, 1 ЮЖНО-АФРИКАНСКИЙ РАНД,ZAR / KZT,26.13,, 100 ЮЖНО-КОРЕЙСКИХ ВОН,KRW / KZT,30.8,, 1 ЯПОНСКАЯ ЙЕНА,JPY / KZT,3.03,, >>>
thank you very much!
your solution much better than my attempts. and now i can go forward.