I am trying to fill up a dataframe with the expected hourly irradiation for an specific location for the next 14 days. I am trying to do scrapping with Selenium from a website where those values are provided, but I do not know how to get the specific values and automate the overall process.
This is what I´ve tried: The df that I want to fill can be generated as follows:
days = range(1,15,1)
hours = ['00:00','01:00','02:00','03:00','04:00','05:00','06:00','07:00','08:00','09:00','10:00','11:00','12:00','13:00','14:00','15:00','16:00','17:00','18:00','19:00','20:00','21:00','22:00','23:00','24:00']
today = date.today()
dates = []
for d in days:
dates.append(today timedelta(days=d))
irradiation = pd.DataFrame(columns=dates, index=hours)
irradiation = irradiation.fillna(0)
** Let me know if you find an easier way to create it.
With this dataframe, and by using Selenium, I want to field up the cells which values are provided in this website 
So for this example, the result would look like:
| Date | 2022-09-07 | ... |
|---|---|---|
| 00:00 | 0 | ... |
| 01:00 | 0 | ... |
| ... | ... | ... |
| 09:00 | 48 | .. |
| 10:00 | 360 | ... |
| ... | ... | ... |
| 24:00 | 0 | ... |
We only fill up the hours where we have values, and they can change one day from other (so one day we have irradiation values from 07:00 and other from 09:00)
This is what I tried:
PATH = "C://Program Files (x86)//chromedriver.exe" # chromedriver.exe path
driver = webdriver.Chrome(PATH)
driver.get("https://www.tutiempo.net/radiacion-solar/madrid.html")
#close cookies
time.sleep(10)
for d in days:
results = driver.find_element(By.ID, "HorasDia" str(d))
# irradiation.loc[[h],[today timedelta(days=(d-1))]] = 0 #here actual value of the irradiation the that hour
print(results.text)
By doing this, I get the hour and the irradiation but I do not know how to iterate each hour and storage that into the df. Does anyone a solution for this?
CodePudding user response:
This is one way you can get that data as a dataframe:
import requests
import pandas as pd
from bs4 import BeautifulSoup as bs
url = 'https://www.tutiempo.net/radiacion-solar/madrid.html'
big_list = []
r = requests.get(url)
soup = bs(r.text, 'html.parser')
days = soup.select('h3')
for x in days:
try:
hours_div = x.find_next('div', {'class': 'horashidsow'})
hours = hours_div.select('div.horhor')
for hour in hours:
h_div = hour.select_one('span.hora')
val_div = hour.select_one('span.ener')
big_list.append((x.text, h_div.text, val_div.text))
except Exception as e:
print('all done, stopping')
break
df = pd.DataFrame(big_list, columns = ['Day', 'Hour', 'Value'])
print(df)
Result:
| Day | Hour | Value | |
|---|---|---|---|
| 0 | Hoy - 7 Septiembre | 09:00 | 48 w/m2 |
| 1 | Hoy - 7 Septiembre | 10:00 | 360 w/m2 |
| 2 | Hoy - 7 Septiembre | 11:00 | 496 w/m2 |
| 3 | Hoy - 7 Septiembre | 12:00 | 166 w/m2 |
| 4 | Hoy - 7 Septiembre | 13:00 | 187 w/m2 |
| 5 | Hoy - 7 Septiembre | 14:00 | 197 w/m2 |
| 6 | Hoy - 7 Septiembre | 15:00 | 193 w/m2 |
| 7 | Hoy - 7 Septiembre | 16:00 | 177 w/m2 |
| 8 | Hoy - 7 Septiembre | 17:00 | 149 w/m2 |
| 9 | Hoy - 7 Septiembre | 18:00 | 135 w/m2 |
| 10 | Hoy - 7 Septiembre | 19:00 | 68 w/m2 |
| 11 | Hoy - 7 Septiembre | 20:00 | 20 w/m2 |
| 12 | Mañana - 8 Septiembre | 09:00 | 47 w/m2 |
| 13 | Mañana - 8 Septiembre | 10:00 | 198 w/m2 |
| 14 | Mañana - 8 Septiembre | 11:00 | 185 w/m2 |
| 15 | Mañana - 8 Septiembre | 12:00 | 165 w/m2 |
| 16 | Mañana - 8 Septiembre | 13:00 | 205 w/m2 |
| 17 | Mañana - 8 Septiembre | 14:00 | 324 w/m2 |
| 18 | Mañana - 8 Septiembre | 15:00 | 211 w/m2 |
| 19 | Mañana - 8 Septiembre | 16:00 | 348 w/m2 |
| 20 | Mañana - 8 Septiembre | 17:00 | 148 w/m2 |
| 21 | Mañana - 8 Septiembre | 18:00 | 184 w/m2 |
| 22 | Mañana - 8 Septiembre | 19:00 | 196 w/m2 |
| 23 | Mañana - 8 Septiembre | 20:00 | 60 w/m2 |
| 24 | Viernes - 9 Septiembre | 09:00 | 185 w/m2 |
| 25 | Viernes - 9 Septiembre | 10:00 | 369 w/m2 |
| 26 | Viernes - 9 Septiembre | 11:00 | 531 w/m2 |
| 27 | Viernes - 9 Septiembre | 12:00 | 658 w/m2 |
| 28 | Viernes - 9 Septiembre | 13:00 | 661 w/m2 |
| 29 | Viernes - 9 Septiembre | 14:00 | 234 w/m2 |
| 30 | Viernes - 9 Septiembre | 15:00 | 191 w/m2 |
| 31 | Viernes - 9 Septiembre | 16:00 | 474 w/m2 |
| 32 | Viernes - 9 Septiembre | 17:00 | 527 w/m2 |
| 33 | Viernes - 9 Septiembre | 18:00 | 433 w/m2 |
| 34 | Viernes - 9 Septiembre | 19:00 | 251 w/m2 |
| 35 | Viernes - 9 Septiembre | 20:00 | 42 w/m2 |
| 36 | Sábado - 10 Septiembre | 09:00 | 182 w/m2 |
| 37 | Sábado - 10 Septiembre | 10:00 | 366 w/m2 |
| 38 | Sábado - 10 Septiembre | 11:00 | 528 w/m2 |
| 39 | Sábado - 10 Septiembre | 12:00 | 655 w/m2 |
| 40 | Sábado - 10 Septiembre | 13:00 | 740 w/m2 |
| 41 | Sábado - 10 Septiembre | 14:00 | 776 w/m2 |
| 42 | Sábado - 10 Septiembre | 15:00 | 760 w/m2 |
| 43 | Sábado - 10 Septiembre | 16:00 | 695 w/m2 |
| 44 | Sábado - 10 Septiembre | 17:00 | 584 w/m2 |
| 45 | Sábado - 10 Septiembre | 18:00 | 435 w/m2 |
| 46 | Sábado - 10 Septiembre | 19:00 | 258 w/m2 |
| 47 | Sábado - 10 Septiembre | 20:00 | 65 w/m2 |
| 48 | Domingo - 11 Septiembre | 09:00 | 179 w/m2 |
| 49 | Domingo - 11 Septiembre | 10:00 | 363 w/m2 |
| 50 | Domingo - 11 Septiembre | 11:00 | 525 w/m2 |
| 51 | Domingo - 11 Septiembre | 12:00 | 652 w/m2 |
| 52 | Domingo - 11 Septiembre | 13:00 | 736 w/m2 |
| 53 | Domingo - 11 Septiembre | 14:00 | 772 w/m2 |
| 54 | Domingo - 11 Septiembre | 15:00 | 756 w/m2 |
| 55 | Domingo - 11 Septiembre | 16:00 | 690 w/m2 |
| 56 | Domingo - 11 Septiembre | 17:00 | 579 w/m2 |
| 57 | Domingo - 11 Septiembre | 18:00 | 430 w/m2 |
| 58 | Domingo - 11 Septiembre | 19:00 | 252 w/m2 |
| 59 | Domingo - 11 Septiembre | 20:00 | 59 w/m2 |
| 60 | Lunes - 12 Septiembre | 09:00 | 44 w/m2 |
| 61 | Lunes - 12 Septiembre | 10:00 | 90 w/m2 |
| 62 | Lunes - 12 Septiembre | 11:00 | 130 w/m2 |
| 63 | Lunes - 12 Septiembre | 12:00 | 162 w/m2 |
| 64 | Lunes - 12 Septiembre | 13:00 | 183 w/m2 |
| 65 | Lunes - 12 Septiembre | 14:00 | 192 w/m2 |
| 66 | Lunes - 12 Septiembre | 15:00 | 188 w/m2 |
| 67 | Lunes - 12 Septiembre | 16:00 | 172 w/m2 |
| 68 | Lunes - 12 Septiembre | 17:00 | 144 w/m2 |
| 69 | Lunes - 12 Septiembre | 18:00 | 241 w/m2 |
| 70 | Lunes - 12 Septiembre | 19:00 | 192 w/m2 |
| 71 | Lunes - 12 Septiembre | 20:00 | 49 w/m2 |
| 72 | Martes - 13 Septiembre | 09:00 | 43 w/m2 |
| 73 | Martes - 13 Septiembre | 10:00 | 89 w/m2 |
| 74 | Martes - 13 Septiembre | 11:00 | 130 w/m2 |
| 75 | Martes - 13 Septiembre | 12:00 | 161 w/m2 |
| 76 | Martes - 13 Septiembre | 13:00 | 182 w/m2 |
| 77 | Martes - 13 Septiembre | 14:00 | 191 w/m2 |
| 78 | Martes - 13 Septiembre | 15:00 | 187 w/m2 |
| 79 | Martes - 13 Septiembre | 16:00 | 170 w/m2 |
| 80 | Martes - 13 Septiembre | 17:00 | 142 w/m2 |
| 81 | Martes - 13 Septiembre | 18:00 | 105 w/m2 |
| 82 | Martes - 13 Septiembre | 19:00 | 60 w/m2 |
| 83 | Martes - 13 Septiembre | 20:00 | 12 w/m2 |
| 84 | Miércoles - 14 Septiembre | 09:00 | 169 w/m2 |
| 85 | Miércoles - 14 Septiembre | 10:00 | 354 w/m2 |
| 86 | Miércoles - 14 Septiembre | 11:00 | 515 w/m2 |
| 87 | Miércoles - 14 Septiembre | 12:00 | 642 w/m2 |
| 88 | Miércoles - 14 Septiembre | 13:00 | 724 w/m2 |
| 89 | Miércoles - 14 Septiembre | 14:00 | 754 w/m2 |
| 90 | Miércoles - 14 Septiembre | 15:00 | 742 w/m2 |
| 91 | Miércoles - 14 Septiembre | 16:00 | 677 w/m2 |
| 92 | Miércoles - 14 Septiembre | 17:00 | 565 w/m2 |
| 93 | Miércoles - 14 Septiembre | 18:00 | 415 w/m2 |
| 94 | Miércoles - 14 Septiembre | 19:00 | 237 w/m2 |
| 95 | Miércoles - 14 Septiembre | 20:00 | 43 w/m2 |
| 96 | Jueves - 15 Septiembre | 09:00 | 166 w/m2 |
| 97 | Jueves - 15 Septiembre | 10:00 | 351 w/m2 |
| 98 | Jueves - 15 Septiembre | 11:00 | 512 w/m2 |
| 99 | Jueves - 15 Septiembre | 12:00 | 639 w/m2 |
| 100 | Jueves - 15 Septiembre | 13:00 | 722 w/m2 |
| 101 | Jueves - 15 Septiembre | 14:00 | 756 w/m2 |
| 102 | Jueves - 15 Septiembre | 15:00 | 739 w/m2 |
| 103 | Jueves - 15 Septiembre | 16:00 | 672 w/m2 |
| 104 | Jueves - 15 Septiembre | 17:00 | 560 w/m2 |
| 105 | Jueves - 15 Septiembre | 18:00 | 409 w/m2 |
| 106 | Jueves - 15 Septiembre | 19:00 | 231 w/m2 |
| 107 | Jueves - 15 Septiembre | 20:00 | 38 w/m2 |
| 108 | Viernes - 16 Septiembre | 09:00 | 41 w/m2 |
| 109 | Viernes - 16 Septiembre | 10:00 | 87 w/m2 |
| 110 | Viernes - 16 Septiembre | 11:00 | 127 w/m2 |
| 111 | Viernes - 16 Septiembre | 12:00 | 159 w/m2 |
| 112 | Viernes - 16 Septiembre | 13:00 | 180 w/m2 |
| 113 | Viernes - 16 Septiembre | 14:00 | 188 w/m2 |
| 114 | Viernes - 16 Septiembre | 15:00 | 238 w/m2 |
| 115 | Viernes - 16 Septiembre | 16:00 | 262 w/m2 |
| 116 | Viernes - 16 Septiembre | 17:00 | 241 w/m2 |
| 117 | Viernes - 16 Septiembre | 18:00 | 159 w/m2 |
| 118 | Viernes - 16 Septiembre | 19:00 | 73 w/m2 |
| 119 | Viernes - 16 Septiembre | 20:00 | 8 w/m2 |
| 120 | Sábado - 17 Septiembre | 09:00 | 159 w/m2 |
| 121 | Sábado - 17 Septiembre | 10:00 | 343 w/m2 |
| 122 | Sábado - 17 Septiembre | 11:00 | 504 w/m2 |
| 123 | Sábado - 17 Septiembre | 12:00 | 631 w/m2 |
| 124 | Sábado - 17 Septiembre | 13:00 | 714 w/m2 |
| 125 | Sábado - 17 Septiembre | 14:00 | 748 w/m2 |
| 126 | Sábado - 17 Septiembre | 15:00 | 731 w/m2 |
| 127 | Sábado - 17 Septiembre | 16:00 | 663 w/m2 |
| 128 | Sábado - 17 Septiembre | 17:00 | 550 w/m2 |
| 129 | Sábado - 17 Septiembre | 18:00 | 399 w/m2 |
| 130 | Sábado - 17 Septiembre | 19:00 | 220 w/m2 |
| 131 | Sábado - 17 Septiembre | 20:00 | 27 w/m2 |
| 132 | Domingo - 18 Septiembre | 09:00 | 157 w/m2 |
| 133 | Domingo - 18 Septiembre | 10:00 | 341 w/m2 |
| 134 | Domingo - 18 Septiembre | 11:00 | 502 w/m2 |
| 135 | Domingo - 18 Septiembre | 12:00 | 627 w/m2 |
| 136 | Domingo - 18 Septiembre | 13:00 | 710 w/m2 |
| 137 | Domingo - 18 Septiembre | 14:00 | 743 w/m2 |
| 138 | Domingo - 18 Septiembre | 15:00 | 720 w/m2 |
| 139 | Domingo - 18 Septiembre | 16:00 | 640 w/m2 |
| 140 | Domingo - 18 Septiembre | 17:00 | 509 w/m2 |
| 141 | Domingo - 18 Septiembre | 18:00 | 342 w/m2 |
| 142 | Domingo - 18 Septiembre | 19:00 | 165 w/m2 |
| 143 | Domingo - 18 Septiembre | 20:00 | 13 w/m2 |
| 144 | Lunes - 19 Septiembre | 09:00 | 154 w/m2 |
| 145 | Lunes - 19 Septiembre | 10:00 | 338 w/m2 |
| 146 | Lunes - 19 Septiembre | 11:00 | 499 w/m2 |
| 147 | Lunes - 19 Septiembre | 12:00 | 624 w/m2 |
| 148 | Lunes - 19 Septiembre | 13:00 | 707 w/m2 |
| 149 | Lunes - 19 Septiembre | 14:00 | 740 w/m2 |
| 150 | Lunes - 19 Septiembre | 15:00 | 722 w/m2 |
| 151 | Lunes - 19 Septiembre | 16:00 | 654 w/m2 |
| 152 | Lunes - 19 Septiembre | 17:00 | 540 w/m2 |
| 153 | Lunes - 19 Septiembre | 18:00 | 389 w/m2 |
| 154 | Lunes - 19 Septiembre | 19:00 | 210 w/m2 |
| 155 | Lunes - 19 Septiembre | 20:00 | 16 w/m2 |
| 156 | Martes - 20 Septiembre | 09:00 | 151 w/m2 |
| 157 | Martes - 20 Septiembre | 10:00 | 335 w/m2 |
| 158 | Martes - 20 Septiembre | 11:00 | 495 w/m2 |
| 159 | Martes - 20 Septiembre | 12:00 | 621 w/m2 |
| 160 | Martes - 20 Septiembre | 13:00 | 703 w/m2 |
| 161 | Martes - 20 Septiembre | 14:00 | 736 w/m2 |
| 162 | Martes - 20 Septiembre | 15:00 | 717 w/m2 |
| 163 | Martes - 20 Septiembre | 16:00 | 649 w/m2 |
| 164 | Martes - 20 Septiembre | 17:00 | 535 w/m2 |
| 165 | Martes - 20 Septiembre | 18:00 | 383 w/m2 |
| 166 | Martes - 20 Septiembre | 19:00 | 204 w/m2 |
| 167 | Martes - 20 Septiembre | 20:00 | 10 w/m2 |
| 168 | Miércoles - 21 Septiembre | 09:00 | 147 w/m2 |
| 169 | Miércoles - 21 Septiembre | 10:00 | 332 w/m2 |
| 170 | Miércoles - 21 Septiembre | 11:00 | 492 w/m2 |
| 171 | Miércoles - 21 Septiembre | 12:00 | 617 w/m2 |
| 172 | Miércoles - 21 Septiembre | 13:00 | 699 w/m2 |
| 173 | Miércoles - 21 Septiembre | 14:00 | 732 w/m2 |
| 174 | Miércoles - 21 Septiembre | 15:00 | 713 w/m2 |
| 175 | Miércoles - 21 Septiembre | 16:00 | 644 w/m2 |
| 176 | Miércoles - 21 Septiembre | 17:00 | 530 w/m2 |
| 177 | Miércoles - 21 Septiembre | 18:00 | 378 w/m2 |
| 178 | Miércoles - 21 Septiembre | 19:00 | 199 w/m2 |
| 179 | Miércoles - 21 Septiembre | 20:00 | 5 w/m2 |
1
Relevant documentation for Requests: https://requests.readthedocs.io/en/latest/
For Pandas: https://pandas.pydata.org/pandas-docs/stable/index.html
And for BeautifulSoup: https://beautiful-soup-4.readthedocs.io/en/latest/index.html
