Spaces:
Build error
Build error
Update tariff_scraper.py
Browse files- tariff_scraper.py +17 -9
tariff_scraper.py
CHANGED
|
@@ -12,22 +12,30 @@ def fetch_tariff_data(url):
|
|
| 12 |
|
| 13 |
tariff_data = {}
|
| 14 |
for section in sections:
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
"Sr. No.": columns[0].get_text(strip=True),
|
| 24 |
"Category": columns[1].get_text(strip=True),
|
| 25 |
"Fixed Rs/Cons/M": columns[2].get_text(strip=True),
|
| 26 |
"Fixed Rs/kW/M": columns[3].get_text(strip=True),
|
| 27 |
"Variable Rs/kWh": columns[4].get_text(strip=True),
|
| 28 |
})
|
|
|
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
|
| 32 |
return tariff_data
|
| 33 |
|
|
|
|
| 12 |
|
| 13 |
tariff_data = {}
|
| 14 |
for section in sections:
|
| 15 |
+
# Get the section heading
|
| 16 |
+
heading_cell = section.find('td')
|
| 17 |
+
if not heading_cell:
|
| 18 |
+
continue
|
| 19 |
+
heading = heading_cell.get_text(strip=True)
|
| 20 |
+
|
| 21 |
+
# Get rows for this section
|
| 22 |
+
rows = []
|
| 23 |
+
sibling = section.find_next_sibling()
|
| 24 |
+
while sibling and sibling.name == 'tr' and sibling.get('id') != 'table_heading':
|
| 25 |
+
columns = sibling.find_all('td')
|
| 26 |
+
if len(columns) >= 5: # Ensure the expected column count
|
| 27 |
+
rows.append({
|
| 28 |
"Sr. No.": columns[0].get_text(strip=True),
|
| 29 |
"Category": columns[1].get_text(strip=True),
|
| 30 |
"Fixed Rs/Cons/M": columns[2].get_text(strip=True),
|
| 31 |
"Fixed Rs/kW/M": columns[3].get_text(strip=True),
|
| 32 |
"Variable Rs/kWh": columns[4].get_text(strip=True),
|
| 33 |
})
|
| 34 |
+
sibling = sibling.find_next_sibling()
|
| 35 |
|
| 36 |
+
# Only add to the data if rows were found
|
| 37 |
+
if rows:
|
| 38 |
+
tariff_data[heading] = pd.DataFrame(rows)
|
| 39 |
|
| 40 |
return tariff_data
|
| 41 |
|