Spaces:
Build error
Build error
Update tariff_scraper.py
Browse files- 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
|
| 7 |
|
| 8 |
Parameters:
|
| 9 |
-
url (str): The URL of the tariff
|
| 10 |
-
Defaults to the IESCO Tariff Guide page.
|
| 11 |
|
| 12 |
Returns:
|
| 13 |
-
|
| 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 |
-
#
|
| 22 |
-
table = soup.find("table")
|
| 23 |
if not table:
|
| 24 |
return "No table found on the page. Please verify the URL."
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
for row in table_rows:
|
| 30 |
-
|
| 31 |
-
if
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
if not
|
| 35 |
-
return "No tariff
|
| 36 |
|
| 37 |
-
return
|
| 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}"
|