Advertisement
Maurizio-Ciullo

funzione macro trend scraping commodities/energy/metals

Oct 2nd, 2021
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.58 KB | None | 0 0
  1. #funzione macro trend scraping commodities/energy/metals
  2. import pandas as pd
  3. import requests
  4. from bs4 import BeautifulSoup
  5.  
  6. #legenda assets
  7. #oil: s_number = 1369 / assets = crude-oil-price-history-chart
  8. #nat_gas: s_number = 2478 / assets = natural-gas-prices-historical-chart
  9. #copper: s_number = 1476 / assets = copper-prices-historical-chart-data
  10. #gold: s_number = 1333 / assets = historical-gold-prices-100-year-chart
  11. #silver: s_number = 1470 / assets = historical-silver-prices-100-year-chart
  12. #platinum: s_number = 2540 / assets = platinum-prices-historical-chart-data
  13.  
  14. def no_dollar (x):
  15.     x = x.replace('$','')
  16.     x = x.replace('%','')
  17.     return x
  18.  
  19. def assets_needed(s_number,assets):
  20.     url = "https://www.macrotrends.net/"+s_number+"/"+assets+""
  21.     #url = "https://www.macrotrends.net/1369/crude-oil-price-history-chart"
  22.     response = requests.get(url)
  23.     print(response)
  24.     if response.status_code != 200:
  25.         print('ERRORE DOWNLOAD PAGINA')
  26.  
  27.     soup = BeautifulSoup(response.content,'html.parser')
  28.     tables = soup.find_all('table',{'class':'table'})
  29.  
  30.     # liste dei parametri
  31.     Years_th = []
  32.     Average_Closing_Price_th = []
  33.     Year_Open_th = []
  34.     Year_High_th = []
  35.     Year_Low_th= []
  36.     Year_Close_th = []
  37.     Annual_Percentual_Change_th = []
  38.  
  39.  
  40.     for table in tables:
  41.         header = table.th.text
  42.         if header.find('Price') == -1:
  43.             continue
  44.         #print(table)
  45.    
  46.  
  47.         rows = table.find_all('tr')
  48.         for row in rows[0:]:
  49.             performances = row.find_all('td')
  50.             for performance in performances:
  51.                 year_td = performances[0].text
  52.                 acp_td = performances[1].text
  53.                 yo_td = performances[2].text
  54.                 yh_td = performances[3].text
  55.                 yl_td = performances[4].text
  56.                 yc_td = performances[5].text
  57.                 apc_td = performances[6].text
  58.                 Years_th.append(year_td)
  59.                 Average_Closing_Price_th.append(acp_td)
  60.                 Year_Open_th.append(yo_td)
  61.                 Year_High_th.append(yh_td)
  62.                 Year_Low_th.append(yl_td)
  63.                 Year_Close_th.append(yc_td)
  64.                 Annual_Percentual_Change_th.append(apc_td)
  65.  
  66.     performance = pd.DataFrame({'Years':Years_th,'ACP':Average_Closing_Price_th,
  67.     'YO':Year_Open_th,'YH':Year_High_th,'YL':Year_Low_th,'YC':Year_Close_th,
  68.     'APC':Annual_Percentual_Change_th})
  69.  
  70.     performance['APC'] = performance['APC'].apply(no_dollar)
  71.     performance['ACP'] = performance['ACP'].apply(no_dollar)
  72.     performance['YO'] = performance['YO'].apply(no_dollar)
  73.     performance['YH'] = performance['YH'].apply(no_dollar)
  74.     performance['YL'] = performance['YL'].apply(no_dollar)
  75.     performance['YC'] = performance['YC'].apply(no_dollar)
  76.     performance['APC'] = pd.to_numeric(performance['APC'])
  77.     performance['ACP'] = pd.to_numeric(performance['ACP'])
  78.     performance['YO'] = pd.to_numeric(performance['YO'])
  79.     performance['YH'] = pd.to_numeric(performance['YH'])
  80.     performance['YL'] = pd.to_numeric(performance['YL'])
  81.     performance['YC'] = pd.to_numeric(performance['YC'])
  82.     performance['Years']= pd.to_datetime(performance['Years'])
  83.     performance.set_index('Years',inplace=True)
  84.     return performance
  85.    
  86.  
  87. #assets_needed("2478","natural-gas-prices-historical-chart")
  88. #assets_needed("1369","crude-oil-price-history-chart")
  89. assets_needed("1476","copper-prices-historical-chart-data").loc['1999':'2020'].plot(y = 'YC')
  90. assets_needed("1476","copper-prices-historical-chart-data").loc['1999':'2020'].min()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement