Spaces:
Sleeping
Sleeping
Update scraper.py
Browse files- scraper.py +18 -3
scraper.py
CHANGED
|
@@ -18,9 +18,24 @@ def parse_tariff_data(soup):
|
|
| 18 |
if len(columns) >= 2: # Ensure there are at least two columns (slab and rate)
|
| 19 |
slab = columns[0].text.strip() # Slab details (e.g., 0-50 kWh)
|
| 20 |
try:
|
| 21 |
-
|
|
|
|
| 22 |
except ValueError:
|
| 23 |
continue # Skip rows that don’t have a valid rate
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
return tariff_data
|
|
|
|
| 18 |
if len(columns) >= 2: # Ensure there are at least two columns (slab and rate)
|
| 19 |
slab = columns[0].text.strip() # Slab details (e.g., 0-50 kWh)
|
| 20 |
try:
|
| 21 |
+
# Attempt to clean and convert rate to a float
|
| 22 |
+
rate = float(columns[1].text.strip().replace(',', '').replace('Rs.', ''))
|
| 23 |
except ValueError:
|
| 24 |
continue # Skip rows that don’t have a valid rate
|
| 25 |
+
|
| 26 |
+
# Handle the case where slab is not a valid numeric range
|
| 27 |
+
slab_range = slab.split('-')
|
| 28 |
+
if len(slab_range) == 2:
|
| 29 |
+
try:
|
| 30 |
+
lower_limit = float(slab_range[0])
|
| 31 |
+
upper_limit = float(slab_range[1])
|
| 32 |
+
tariff_data.append({'slab': slab, 'rate': rate, 'lower_limit': lower_limit, 'upper_limit': upper_limit})
|
| 33 |
+
except ValueError:
|
| 34 |
+
continue # Skip slabs that are not valid numeric ranges
|
| 35 |
+
elif len(slab_range) == 1:
|
| 36 |
+
try:
|
| 37 |
+
lower_limit = float(slab_range[0])
|
| 38 |
+
tariff_data.append({'slab': slab, 'rate': rate, 'lower_limit': lower_limit, 'upper_limit': float('inf')})
|
| 39 |
+
except ValueError:
|
| 40 |
+
continue # Skip slabs that are not valid numeric values
|
| 41 |
return tariff_data
|