Engineer786 commited on
Commit
347c283
·
verified ·
1 Parent(s): 974a0ae

Update tariff_scraper.py

Browse files
Files changed (1) hide show
  1. 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
- # Fetch the webpage
10
- response = requests.get(url)
11
- response.raise_for_status()
12
  soup = BeautifulSoup(response.text, 'html.parser')
13
 
14
- # Find all rows in the table with the specific structure
15
- rows = soup.find_all("tr", id=lambda x: x and x.startswith("table_")) # Matches rows like "table_even_row"
 
16
 
17
- if not rows:
18
- return "Error: No rows found in the tariff table."
 
 
 
 
 
 
 
19
 
20
- # Parse the rows
21
- tariff_data = []
22
- for row in rows:
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
- if not tariff_data:
32
- return "Error: No meaningful data found in the tariff table."
 
33
 
34
- return tariff_data
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
- except Exception as e:
37
- return f"An error occurred while scraping: {e}"
 
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()