Spaces:
Build error
Build error
Update tariff_scraper.py
Browse files- tariff_scraper.py +38 -25
tariff_scraper.py
CHANGED
|
@@ -1,37 +1,50 @@
|
|
|
|
|
| 1 |
import requests
|
| 2 |
from bs4 import BeautifulSoup
|
| 3 |
|
| 4 |
def scrape_tariff_data(url):
|
| 5 |
-
"""
|
| 6 |
-
Scrape tariff data from the provided URL.
|
| 7 |
-
"""
|
| 8 |
try:
|
| 9 |
-
|
| 10 |
-
response
|
| 11 |
-
response.raise_for_status()
|
| 12 |
soup = BeautifulSoup(response.text, 'html.parser')
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
# Extract all cell values (text inside <td>)
|
| 24 |
-
cells = row.find_all("td")
|
| 25 |
-
row_data = [cell.get_text(strip=True) for cell in cells]
|
| 26 |
-
|
| 27 |
-
# Only include rows with meaningful data (exclude rows with all `-`)
|
| 28 |
-
if any(cell != '-' for cell in row_data):
|
| 29 |
-
tariff_data.append(row_data)
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
|
|
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
import requests
|
| 3 |
from bs4 import BeautifulSoup
|
| 4 |
|
| 5 |
def scrape_tariff_data(url):
|
|
|
|
|
|
|
|
|
|
| 6 |
try:
|
| 7 |
+
response = requests.get(url, headers={"User-Agent": "Mozilla/5.0"})
|
| 8 |
+
response.raise_for_status() # Raise an error for bad responses
|
|
|
|
| 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 |
+
data = []
|
| 16 |
+
for section in tariff_sections:
|
| 17 |
+
table_rows = section.find_all('tr')
|
| 18 |
+
for row in table_rows:
|
| 19 |
+
row_text = ' | '.join(
|
| 20 |
+
col.get_text(strip=True) for col in row.find_all(['th', 'td'])
|
| 21 |
+
)
|
| 22 |
+
if row_text: # Add the row text only if it contains data
|
| 23 |
+
data.append(row_text)
|
| 24 |
|
| 25 |
+
return data # Returns a list of row strings
|
| 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 = scrape_tariff_data(url)
|
| 39 |
+
if isinstance(data, list):
|
| 40 |
+
st.success("Data scraped successfully!")
|
| 41 |
+
st.write("Here is a preview of the data:")
|
| 42 |
+
for row in data[:10]: # Show only the first 10 rows for readability
|
| 43 |
+
st.write(row)
|
| 44 |
+
else:
|
| 45 |
+
st.error(data)
|
| 46 |
+
else:
|
| 47 |
+
st.error("Please enter a valid URL.")
|
| 48 |
|
| 49 |
+
if __name__ == "__main__":
|
| 50 |
+
main()
|