Engineer786 commited on
Commit
0b42826
·
verified ·
1 Parent(s): 44c0124

Update tariff_scraper.py

Browse files
Files changed (1) hide show
  1. tariff_scraper.py +16 -10
tariff_scraper.py CHANGED
@@ -2,29 +2,35 @@ import requests
2
  from bs4 import BeautifulSoup
3
 
4
  def scrape_tariff_data(url):
 
 
 
5
  try:
6
  # Fetch the webpage
7
  response = requests.get(url)
8
  response.raise_for_status()
9
  soup = BeautifulSoup(response.text, 'html.parser')
10
 
11
- # Find the table containing tariff data
12
- table = soup.find("table") # Adjust this if the table has specific attributes like class or id
13
- if not table:
14
- return "Error: Could not find the tariff table on the page."
15
 
16
- # Extract table rows
17
- rows = table.find_all("tr")
18
  if not rows:
19
  return "Error: No rows found in the tariff table."
20
 
21
- # Parse table rows and extract data
22
  tariff_data = []
23
  for row in rows:
24
- cells = row.find_all(["td", "th"]) # Include both header and data cells
25
- tariff_data.append([cell.get_text(strip=True) for cell in cells])
 
 
 
 
 
 
 
 
26
 
27
- # Return the tariff data as a list of lists (rows)
28
  return tariff_data
29
 
30
  except Exception as e:
 
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: