Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,45 +2,50 @@ import streamlit as st
|
|
| 2 |
import requests
|
| 3 |
from bs4 import BeautifulSoup
|
| 4 |
|
| 5 |
-
def
|
| 6 |
try:
|
| 7 |
response = requests.get(url, headers={"User-Agent": "Mozilla/5.0"})
|
| 8 |
-
response.raise_for_status()
|
| 9 |
soup = BeautifulSoup(response.text, 'html.parser')
|
| 10 |
|
| 11 |
# Extract specific elements based on the webpage structure
|
| 12 |
-
# Assume tariff data is in <table> tags
|
| 13 |
tariff_sections = soup.find_all('table')
|
| 14 |
|
| 15 |
-
|
| 16 |
for section in tariff_sections:
|
| 17 |
table_rows = section.find_all('tr')
|
| 18 |
for row in table_rows:
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
return
|
| 26 |
except Exception as e:
|
| 27 |
return f"An error occurred: {e}"
|
| 28 |
|
| 29 |
def main():
|
| 30 |
-
st.title("Electricity Tariff Scraper")
|
| 31 |
st.write("Enter the URL of the electricity tariff page:")
|
| 32 |
|
| 33 |
url = st.text_input("URL", "https://iesco.com.pk/index.php/customer-services/tariff-guide")
|
| 34 |
|
| 35 |
-
if st.button("Scrape"):
|
| 36 |
if url:
|
| 37 |
with st.spinner("Scraping data..."):
|
| 38 |
-
data =
|
| 39 |
if isinstance(data, list):
|
| 40 |
-
st.success("Data scraped successfully!")
|
| 41 |
-
st.write("Here is a preview of the data:")
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
else:
|
| 45 |
st.error(data)
|
| 46 |
else:
|
|
|
|
| 2 |
import requests
|
| 3 |
from bs4 import BeautifulSoup
|
| 4 |
|
| 5 |
+
def scrape_and_clean_tariff_data(url):
|
| 6 |
try:
|
| 7 |
response = requests.get(url, headers={"User-Agent": "Mozilla/5.0"})
|
| 8 |
+
response.raise_for_status()
|
| 9 |
soup = BeautifulSoup(response.text, 'html.parser')
|
| 10 |
|
| 11 |
# Extract specific elements based on the webpage structure
|
|
|
|
| 12 |
tariff_sections = soup.find_all('table')
|
| 13 |
|
| 14 |
+
cleaned_data = []
|
| 15 |
for section in tariff_sections:
|
| 16 |
table_rows = section.find_all('tr')
|
| 17 |
for row in table_rows:
|
| 18 |
+
# Extract text for each cell and handle missing/empty values
|
| 19 |
+
row_data = [col.get_text(strip=True) if col.get_text(strip=True) else "N/A"
|
| 20 |
+
for col in row.find_all(['th', 'td'])]
|
| 21 |
+
|
| 22 |
+
# Skip rows that are completely empty
|
| 23 |
+
if any(cell != "N/A" for cell in row_data):
|
| 24 |
+
cleaned_data.append(row_data)
|
| 25 |
|
| 26 |
+
return cleaned_data # Returns a list of cleaned rows
|
| 27 |
except Exception as e:
|
| 28 |
return f"An error occurred: {e}"
|
| 29 |
|
| 30 |
def main():
|
| 31 |
+
st.title("Electricity Tariff Scraper with Data Cleaning")
|
| 32 |
st.write("Enter the URL of the electricity tariff page:")
|
| 33 |
|
| 34 |
url = st.text_input("URL", "https://iesco.com.pk/index.php/customer-services/tariff-guide")
|
| 35 |
|
| 36 |
+
if st.button("Scrape and Clean"):
|
| 37 |
if url:
|
| 38 |
with st.spinner("Scraping data..."):
|
| 39 |
+
data = scrape_and_clean_tariff_data(url)
|
| 40 |
if isinstance(data, list):
|
| 41 |
+
st.success("Data scraped and cleaned successfully!")
|
| 42 |
+
st.write("Here is a preview of the cleaned data:")
|
| 43 |
+
|
| 44 |
+
# Show cleaned data in a readable format
|
| 45 |
+
for row in data[:10]: # Show first 10 rows
|
| 46 |
+
st.write(" | ".join(row))
|
| 47 |
+
|
| 48 |
+
st.write(f"Total rows cleaned: {len(data)}")
|
| 49 |
else:
|
| 50 |
st.error(data)
|
| 51 |
else:
|