Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Agregar cotizacion dolar oficial #32

Open
arielmeragelman opened this issue Jun 3, 2022 · 0 comments
Open

Agregar cotizacion dolar oficial #32

arielmeragelman opened this issue Jun 3, 2022 · 0 comments

Comments

@arielmeragelman
Copy link

OBTENGO 2 DATAFRAME CON LOS VALORES HISTORICOS DEL DOLAR EN ARGENTINA

import requests
from bs4 import BeautifulSoup
import datetime
import pandas as pd

def scrap(año, mes, url):

for i in range(1,7):
    try:        
        fecha = datetime.datetime(año,mes,i)
        data = {'fecha': fecha.strftime('%d-%m-%y')}
        resp = requests.post(url, data=data)
        soup = BeautifulSoup(resp.text, "html.parser")
        break
    except:
        print('Falló en ',i)    
filas = soup.find_all('td', {'style' : 'padding: 1%'})
return filas

def parsear(filas):
mensual = pd.DataFrame()
for i in range(1, int(len(list(filas))/3)):
dic = {}
dic['fecha'] = filas[3i].text
dic['bid'] = filas[3
i+1].text
dic['ask'] = filas[3*i+2].text
rueda = pd.DataFrame.from_dict(dic, orient='index').transpose().set_index('fecha')
rueda.index = pd.to_datetime(rueda.index, format='%d-%m-%y ')
mensual = pd.concat([mensual,rueda], axis=0)
return mensual

def downloadAño(año,url):
tablaAnual = pd.DataFrame()
for i in range(1,13):
filas = scrap(año=año, mes=i,url=url)
tabla = parsear(filas)
tablaAnual = pd.concat([tablaAnual,tabla],axis=0)
print('mes',i,'listo')
tablaAnual.to_excel('blue_'+str(año)+'.xlsx')
print(tablaAnual)
return tablaAnual

año=2022

dolar_blue= downloadAño(año,'https://www.cotizacion-dolar.com.ar/dolar-blue-historico-'+str(año)+'.php')
dolar_oficial= downloadAño(año,'https://www.cotizacion-dolar.com.ar/dolar-historico-bna-'+str(año)+'.php')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant