Engineer786 commited on
Commit
4915bbd
·
verified ·
1 Parent(s): abbbe18

Create tariff_scraper.py

Browse files
Files changed (1) hide show
  1. tariff_scraper.py +46 -0
tariff_scraper.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ from bs4 import BeautifulSoup
3
+ import pandas as pd
4
+
5
+ def fetch_tariff_data(url):
6
+ response = requests.get(url)
7
+ if response.status_code != 200:
8
+ raise Exception(f"Failed to fetch data from {url}, status code: {response.status_code}")
9
+
10
+ soup = BeautifulSoup(response.content, 'html.parser')
11
+ sections = soup.find_all('tr', id='table_heading')
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
+
34
+ def save_tariff_data(tariff_data, file_path):
35
+ with pd.ExcelWriter(file_path) as writer:
36
+ for heading, df in tariff_data.items():
37
+ df.to_excel(writer, sheet_name=heading[:31], index=False)
38
+
39
+ if __name__ == "__main__":
40
+ url = "https://iesco.com.pk/index.php/customer-services/tariff-guide"
41
+ try:
42
+ tariff_data = fetch_tariff_data(url)
43
+ save_tariff_data(tariff_data, "tariff_data.xlsx")
44
+ print("Tariff data successfully saved to tariff_data.xlsx")
45
+ except Exception as e:
46
+ print(f"Error: {e}")