File size: 1,547 Bytes
71520e8
55a5323
3a49fce
 
55a5323
71520e8
 
 
 
 
9d37e34
 
 
 
 
 
71520e8
9d37e34
 
71520e8
 
 
9d37e34
71520e8
9d37e34
 
 
 
 
 
 
 
 
71520e8
 
 
 
 
 
 
 
 
9943ae4
866dcf1
71520e8
 
9d37e34
71520e8
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# scraper.py
import requests
from bs4 import BeautifulSoup

def fetch_tariff_data(url):
    try:
        response = requests.get(url)
        response.raise_for_status()
        soup = BeautifulSoup(response.content, 'html.parser')
        
        # Locate the table with tariff data
        table = soup.find('table')
        if not table:
            print("Error: No table found on the page.")
            return None

        tariff_data = []
        rows = table.find_all('tr')[1:]  # Skip the header row

        for row in rows:
            cols = row.find_all('td')
            if len(cols) >= 2:
                particulars = cols[0].text.strip()
                rate = parse_float(cols[-1].text.strip())

                if "Units" in particulars:
                    limits = [parse_float(x) for x in particulars.split(' ')[1:]]
                    lower_limit = limits[0]
                    upper_limit = limits[1] if len(limits) > 1 else float('inf')
                else:
                    lower_limit = 0
                    upper_limit = float('inf')

                tariff_data.append({
                    'lower_limit': lower_limit,
                    'upper_limit': upper_limit,
                    'rate': rate
                })
        
        return tariff_data
    except Exception as e:
        print(f"Error fetching tariff data: {e}")
        return None

def parse_float(value):
    try:
        return float(value.replace(',', '').replace('PKR', '').strip())
    except ValueError:
        return float('inf')