Engineer786 commited on
Commit
683037f
·
verified ·
1 Parent(s): e2914c1

Update tariff_scraper.py

Browse files
Files changed (1) hide show
  1. tariff_scraper.py +15 -17
tariff_scraper.py CHANGED
@@ -12,30 +12,22 @@ def fetch_tariff_data(url):
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
 
@@ -52,3 +44,9 @@ if __name__ == "__main__":
52
  print("Tariff data successfully saved to tariff_data.xlsx")
53
  except Exception as e:
54
  print(f"Error: {e}")
 
 
 
 
 
 
 
12
 
13
  tariff_data = {}
14
  for section in sections:
15
+ heading = section.find('td').get_text(strip=True)
16
+ rows = section.find_next_siblings('tr')
17
+
18
+ data = []
19
+ for row in rows:
20
+ columns = row.find_all('td')
21
+ if len(columns) >= 5: # Ensure it has the expected number of columns
22
+ data.append({
 
 
 
 
 
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
+ tariff_data[heading] = pd.DataFrame(data)
 
 
31
 
32
  return tariff_data
33
 
 
44
  print("Tariff data successfully saved to tariff_data.xlsx")
45
  except Exception as e:
46
  print(f"Error: {e}")
47
+
48
+
49
+
50
+
51
+
52
+