Spaces:
Build error
Build error
Update tariff_scraper.py
Browse files- tariff_scraper.py +24 -20
tariff_scraper.py
CHANGED
|
@@ -1,20 +1,22 @@
|
|
| 1 |
import requests
|
| 2 |
from bs4 import BeautifulSoup
|
|
|
|
| 3 |
|
| 4 |
# Define the URL for PESCO tariff rates
|
| 5 |
TARIFF_URLS = {
|
| 6 |
"PESCO": "https://onlinepescobill.pk/pesco-tariff-rates/"
|
| 7 |
}
|
| 8 |
|
| 9 |
-
def
|
| 10 |
"""
|
| 11 |
-
Scrape tariff data from the given URL.
|
| 12 |
|
| 13 |
Args:
|
| 14 |
url (str): The URL of the tariff page to scrape.
|
|
|
|
| 15 |
|
| 16 |
Returns:
|
| 17 |
-
|
| 18 |
"""
|
| 19 |
try:
|
| 20 |
# Send an HTTP GET request to the specified URL
|
|
@@ -27,31 +29,33 @@ def scrape_tariff_data(url):
|
|
| 27 |
# Extract table rows
|
| 28 |
tariff_table = soup.find('table')
|
| 29 |
if not tariff_table:
|
| 30 |
-
return
|
| 31 |
|
|
|
|
| 32 |
data = []
|
| 33 |
table_rows = tariff_table.find_all('tr')
|
| 34 |
for row in table_rows:
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
return
|
| 43 |
except requests.exceptions.RequestException as e:
|
| 44 |
# Handle request errors (e.g., connection issues, timeout)
|
| 45 |
-
return
|
| 46 |
except Exception as e:
|
| 47 |
# Handle other potential errors
|
| 48 |
-
return
|
| 49 |
|
| 50 |
if __name__ == "__main__":
|
| 51 |
-
# Test the scraper
|
| 52 |
url = TARIFF_URLS["PESCO"]
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
| 1 |
import requests
|
| 2 |
from bs4 import BeautifulSoup
|
| 3 |
+
import pandas as pd
|
| 4 |
|
| 5 |
# Define the URL for PESCO tariff rates
|
| 6 |
TARIFF_URLS = {
|
| 7 |
"PESCO": "https://onlinepescobill.pk/pesco-tariff-rates/"
|
| 8 |
}
|
| 9 |
|
| 10 |
+
def scrape_tariff_data_to_csv(url, output_file="pesco_tariff_data.csv"):
|
| 11 |
"""
|
| 12 |
+
Scrape tariff data from the given URL and save it to a CSV file.
|
| 13 |
|
| 14 |
Args:
|
| 15 |
url (str): The URL of the tariff page to scrape.
|
| 16 |
+
output_file (str): The name of the CSV file to save data.
|
| 17 |
|
| 18 |
Returns:
|
| 19 |
+
str: The name of the CSV file if successful, or an error message.
|
| 20 |
"""
|
| 21 |
try:
|
| 22 |
# Send an HTTP GET request to the specified URL
|
|
|
|
| 29 |
# Extract table rows
|
| 30 |
tariff_table = soup.find('table')
|
| 31 |
if not tariff_table:
|
| 32 |
+
return "No table found on the webpage."
|
| 33 |
|
| 34 |
+
# Extract data and convert it into a structured format
|
| 35 |
data = []
|
| 36 |
table_rows = tariff_table.find_all('tr')
|
| 37 |
for row in table_rows:
|
| 38 |
+
cols = [col.get_text(strip=True) for col in row.find_all(['th', 'td'])]
|
| 39 |
+
data.append(cols)
|
| 40 |
+
|
| 41 |
+
# Save the data to a CSV file
|
| 42 |
+
df = pd.DataFrame(data[1:], columns=data[0]) # Use the first row as headers
|
| 43 |
+
df.to_csv(output_file, index=False)
|
| 44 |
+
|
| 45 |
+
return output_file
|
| 46 |
except requests.exceptions.RequestException as e:
|
| 47 |
# Handle request errors (e.g., connection issues, timeout)
|
| 48 |
+
return f"Request error: {e}"
|
| 49 |
except Exception as e:
|
| 50 |
# Handle other potential errors
|
| 51 |
+
return f"An unexpected error occurred: {e}"
|
| 52 |
|
| 53 |
if __name__ == "__main__":
|
| 54 |
+
# Test the scraper and save data to a CSV file
|
| 55 |
url = TARIFF_URLS["PESCO"]
|
| 56 |
+
output_file = "pesco_tariff_data.csv"
|
| 57 |
+
result = scrape_tariff_data_to_csv(url, output_file)
|
| 58 |
+
if result.endswith(".csv"):
|
| 59 |
+
print(f"Data successfully saved to {output_file}")
|
| 60 |
+
else:
|
| 61 |
+
print(f"Error: {result}")
|