Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,14 +4,23 @@ from bs4 import BeautifulSoup
|
|
| 4 |
|
| 5 |
def scrape_tariff_data(url):
|
| 6 |
try:
|
| 7 |
-
response = requests.get(url)
|
| 8 |
response.raise_for_status() # Raise an error for bad responses
|
| 9 |
soup = BeautifulSoup(response.text, 'html.parser')
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
except Exception as e:
|
| 16 |
return f"An error occurred: {e}"
|
| 17 |
|
|
@@ -19,7 +28,7 @@ def main():
|
|
| 19 |
st.title("Electricity Tariff Scraper")
|
| 20 |
st.write("Enter the URL of the electricity tariff page:")
|
| 21 |
|
| 22 |
-
url = st.text_input("URL", "")
|
| 23 |
|
| 24 |
if st.button("Scrape"):
|
| 25 |
if url:
|
|
@@ -27,11 +36,13 @@ def main():
|
|
| 27 |
data = scrape_tariff_data(url)
|
| 28 |
if isinstance(data, list):
|
| 29 |
st.success("Data scraped successfully!")
|
| 30 |
-
st.write(data)
|
|
|
|
|
|
|
| 31 |
else:
|
| 32 |
st.error(data)
|
| 33 |
else:
|
| 34 |
st.error("Please enter a valid URL.")
|
| 35 |
|
| 36 |
if __name__ == "__main__":
|
| 37 |
-
main()
|
|
|
|
| 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 |
+
# Replace 'div' and class with the actual elements where tariff data resides
|
| 13 |
+
tariff_sections = soup.find_all('table') # Assume tariff data is in tables
|
| 14 |
+
|
| 15 |
+
data = []
|
| 16 |
+
for section in tariff_sections:
|
| 17 |
+
table_rows = section.find_all('tr')
|
| 18 |
+
for row in table_rows:
|
| 19 |
+
columns = [col.get_text(strip=True) for col in row.find_all(['th', 'td'])]
|
| 20 |
+
if columns:
|
| 21 |
+
data.append(columns)
|
| 22 |
+
|
| 23 |
+
return data # Returns a list of rows with tariff data
|
| 24 |
except Exception as e:
|
| 25 |
return f"An error occurred: {e}"
|
| 26 |
|
|
|
|
| 28 |
st.title("Electricity Tariff Scraper")
|
| 29 |
st.write("Enter the URL of the electricity tariff page:")
|
| 30 |
|
| 31 |
+
url = st.text_input("URL", "https://iesco.com.pk/index.php/customer-services/tariff-guide")
|
| 32 |
|
| 33 |
if st.button("Scrape"):
|
| 34 |
if url:
|
|
|
|
| 36 |
data = scrape_tariff_data(url)
|
| 37 |
if isinstance(data, list):
|
| 38 |
st.success("Data scraped successfully!")
|
| 39 |
+
st.write("Here is a preview of the data:")
|
| 40 |
+
for row in data[:10]: # Show only the first 10 rows for readability
|
| 41 |
+
st.write(row)
|
| 42 |
else:
|
| 43 |
st.error(data)
|
| 44 |
else:
|
| 45 |
st.error("Please enter a valid URL.")
|
| 46 |
|
| 47 |
if __name__ == "__main__":
|
| 48 |
+
main()
|