Engineer786 commited on
Commit
90042c9
·
verified ·
1 Parent(s): 2b4aa81

Update tariff_scraper.py

Browse files
Files changed (1) hide show
  1. tariff_scraper.py +22 -15
tariff_scraper.py CHANGED
@@ -3,14 +3,13 @@ from bs4 import BeautifulSoup
3
 
4
  def scrape_tariff_data(url="https://iesco.com.pk/index.php/customer-services/tariff-guide"):
5
  """
6
- Scrapes tariff data from the IESCO tariff guide page.
7
 
8
  Parameters:
9
- url (str): The URL of the tariff data page.
10
- Defaults to the IESCO Tariff Guide page.
11
 
12
  Returns:
13
- list: A list of rows containing tariff-related data as strings.
14
  str: Error message if scraping fails.
15
  """
16
  try:
@@ -18,22 +17,30 @@ def scrape_tariff_data(url="https://iesco.com.pk/index.php/customer-services/tar
18
  response.raise_for_status() # Raise error for bad responses
19
  soup = BeautifulSoup(response.text, 'html.parser')
20
 
21
- # Assuming the tariff data is stored in table rows
22
- table = soup.find("table") # Locate the main table
23
  if not table:
24
  return "No table found on the page. Please verify the URL."
25
 
26
- table_rows = table.find_all("tr") # Extract all table rows
27
- scraped_data = []
28
-
29
  for row in table_rows:
30
- row_data = [cell.get_text(strip=True) for cell in row.find_all(["th", "td"])]
31
- if row_data: # Only add rows with data
32
- scraped_data.append(" | ".join(row_data))
 
 
 
 
 
 
 
 
33
 
34
- if not scraped_data:
35
- return "No tariff data found in the table. Please check the page structure."
36
 
37
- return scraped_data
38
  except Exception as e:
39
  return f"An error occurred while scraping: {e}"
 
3
 
4
  def scrape_tariff_data(url="https://iesco.com.pk/index.php/customer-services/tariff-guide"):
5
  """
6
+ Scrapes tariff rates from the IESCO tariff guide page.
7
 
8
  Parameters:
9
+ url (str): The URL of the tariff guide page.
 
10
 
11
  Returns:
12
+ dict: A dictionary containing tariff rates for different categories.
13
  str: Error message if scraping fails.
14
  """
15
  try:
 
17
  response.raise_for_status() # Raise error for bad responses
18
  soup = BeautifulSoup(response.text, 'html.parser')
19
 
20
+ # Locate the main table containing the tariff data
21
+ table = soup.find("table")
22
  if not table:
23
  return "No table found on the page. Please verify the URL."
24
 
25
+ # Extract tariff data into a dictionary
26
+ tariff_rates = {}
27
+ table_rows = table.find_all("tr") # Get all rows in the table
28
  for row in table_rows:
29
+ cells = row.find_all("td")
30
+ if len(cells) >= 2: # Ensure row has enough columns (category and rate)
31
+ category = cells[0].get_text(strip=True)
32
+ rate = cells[1].get_text(strip=True)
33
+
34
+ # Parse rate into a float
35
+ try:
36
+ rate_value = float(rate.replace(",", "").split()[0]) # Clean and parse rate
37
+ tariff_rates[category] = rate_value
38
+ except ValueError:
39
+ continue # Skip rows with invalid rate values
40
 
41
+ if not tariff_rates:
42
+ return "No valid tariff rates found in the table."
43
 
44
+ return tariff_rates
45
  except Exception as e:
46
  return f"An error occurred while scraping: {e}"