گرفتن قیمت یورو چندسال گذشته

از سایت بن بست با پایتون

from lxml import etree
import urllib
import pandas as pd

امتحان کانکشن

web = urllib.request.urlopen("https://www.bonbast.com/historical/eur/2020/10")
s = web.read()

html = etree.HTML(s)

## Get all 'tr'
tr_nodes = html.xpath('//table/tr')

## 'th' is inside first 'tr'
# header = [i[0].text for i in tr_nodes[0].xpath("th")]

## Get text from rest all 'tr'
td_content = [[td.text for td in tr.xpath('td')] for tr in tr_nodes[1:-4]]
df = pd.DataFrame(td_content)
df.columns = ['Date', 'Sell', 'Buy']
df

گرفتن مقادیر

for year in range(2013,2021):
    for month in range(1,13):
        web1 = urllib.request.urlopen("https://www.bonbast.com/historical/eur/"+str(year)+"/"+str(month))
        s1 = web1.read()
        html1 = etree.HTML(s1)
        tr_nodes1 = html1.xpath('//table/tr')
        td_content1 = [[td.text for td in tr.xpath('td')] for tr in tr_nodes1[1:-4]]
        df1 = pd.DataFrame(td_content1)
        df1.columns = ['Date', 'Sell', 'Buy']
        df = df.append(df1 , ignore_index=True)
df

با تاریخ شمسی

dt = pd.read_csv('..\..\General\DimDate.csv')
dt = dt[['DateKey', 'FullDateAlternateKey',  'ShamsiDateKey', 'ShamsiDate' 
        ,'ShamsiDayNumberOfWeek', 'ShamsiDayNameOfWeek' ,'ShamsiDayNumberOfMonth'
        , 'ShamsiDayNumberOfYear', 'ShamsiMonthName','ShamsiMonthNumberOfYear', 'ShamsiQuarter', 'ShamsiYear'
       ,'ShamsiYearMonth', 'ShamsiMonthNumberOfDays', 'IsShamsiHoliday' ,'ShamsiWeekNumberOfYear']]
df = df.merge(dt , left_on='Date' , right_on='FullDateAlternateKey' )

خروجی اکسل

df.to_excel('Eurofrom2013To202102.xlsx')

Leave a comment

Your email address will not be published. Required fields are marked *