Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #funzione macro trend scraping commodities/energy/metals
- import pandas as pd
- import requests
- from bs4 import BeautifulSoup
- #legenda assets
- #oil: s_number = 1369 / assets = crude-oil-price-history-chart
- #nat_gas: s_number = 2478 / assets = natural-gas-prices-historical-chart
- #copper: s_number = 1476 / assets = copper-prices-historical-chart-data
- #gold: s_number = 1333 / assets = historical-gold-prices-100-year-chart
- #silver: s_number = 1470 / assets = historical-silver-prices-100-year-chart
- #platinum: s_number = 2540 / assets = platinum-prices-historical-chart-data
- def no_dollar (x):
- x = x.replace('$','')
- x = x.replace('%','')
- return x
- def assets_needed(s_number,assets):
- url = "https://www.macrotrends.net/"+s_number+"/"+assets+""
- #url = "https://www.macrotrends.net/1369/crude-oil-price-history-chart"
- response = requests.get(url)
- print(response)
- if response.status_code != 200:
- print('ERRORE DOWNLOAD PAGINA')
- soup = BeautifulSoup(response.content,'html.parser')
- tables = soup.find_all('table',{'class':'table'})
- # liste dei parametri
- Years_th = []
- Average_Closing_Price_th = []
- Year_Open_th = []
- Year_High_th = []
- Year_Low_th= []
- Year_Close_th = []
- Annual_Percentual_Change_th = []
- for table in tables:
- header = table.th.text
- if header.find('Price') == -1:
- continue
- #print(table)
- rows = table.find_all('tr')
- for row in rows[0:]:
- performances = row.find_all('td')
- for performance in performances:
- year_td = performances[0].text
- acp_td = performances[1].text
- yo_td = performances[2].text
- yh_td = performances[3].text
- yl_td = performances[4].text
- yc_td = performances[5].text
- apc_td = performances[6].text
- Years_th.append(year_td)
- Average_Closing_Price_th.append(acp_td)
- Year_Open_th.append(yo_td)
- Year_High_th.append(yh_td)
- Year_Low_th.append(yl_td)
- Year_Close_th.append(yc_td)
- Annual_Percentual_Change_th.append(apc_td)
- performance = pd.DataFrame({'Years':Years_th,'ACP':Average_Closing_Price_th,
- 'YO':Year_Open_th,'YH':Year_High_th,'YL':Year_Low_th,'YC':Year_Close_th,
- 'APC':Annual_Percentual_Change_th})
- performance['APC'] = performance['APC'].apply(no_dollar)
- performance['ACP'] = performance['ACP'].apply(no_dollar)
- performance['YO'] = performance['YO'].apply(no_dollar)
- performance['YH'] = performance['YH'].apply(no_dollar)
- performance['YL'] = performance['YL'].apply(no_dollar)
- performance['YC'] = performance['YC'].apply(no_dollar)
- performance['APC'] = pd.to_numeric(performance['APC'])
- performance['ACP'] = pd.to_numeric(performance['ACP'])
- performance['YO'] = pd.to_numeric(performance['YO'])
- performance['YH'] = pd.to_numeric(performance['YH'])
- performance['YL'] = pd.to_numeric(performance['YL'])
- performance['YC'] = pd.to_numeric(performance['YC'])
- performance['Years']= pd.to_datetime(performance['Years'])
- performance.set_index('Years',inplace=True)
- return performance
- #assets_needed("2478","natural-gas-prices-historical-chart")
- #assets_needed("1369","crude-oil-price-history-chart")
- assets_needed("1476","copper-prices-historical-chart-data").loc['1999':'2020'].plot(y = 'YC')
- assets_needed("1476","copper-prices-historical-chart-data").loc['1999':'2020'].min()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement