|
| 1 | +import requests |
| 2 | +from bs4 import BeautifulSoup |
| 3 | +import tkinter as tk |
| 4 | +from tkinter import ttk |
| 5 | + |
| 6 | +# Dictionary containing category and corresponding slug value |
| 7 | +categories = {'Most Runs': 'most-runs', |
| 8 | + 'Most Fours': 'most-fours', 'Most Sixes': 'most-sixes', 'Most Fifties': 'most-fifties', |
| 9 | + 'Most Centuries': 'most-centuries', 'Highest Scores': 'highest-scores', 'Most Wickets': 'most-wickets', |
| 10 | + 'Most Maidens': 'most-maidens', 'Most Dot Balls': 'most-dot-balls', 'Best Bowling Average': 'best-bowling-average', |
| 11 | + 'Best Bowling Economy': 'best-bowling-economy', 'Best Bowling Strike Rate': 'best-bowling-strike-rate'} |
| 12 | + |
| 13 | +# Function to generate request url based on user choice |
| 14 | +def generate_url(): |
| 15 | + category_choice = category.get() |
| 16 | + year_choice = year.get() |
| 17 | + if (year_choice == 'All time'): |
| 18 | + year_choice = 'all-time' |
| 19 | + category_slug = categories[category_choice] |
| 20 | + url = 'https://www.iplt20.com/stats/{}/{}'.format( |
| 21 | + year_choice, category_slug) |
| 22 | + return url |
| 23 | + |
| 24 | +# Function to scrape results based on request url |
| 25 | +def scrape_results(): |
| 26 | + url = generate_url() |
| 27 | + page = requests.get(url) |
| 28 | + |
| 29 | + # Start scraping resultant html data |
| 30 | + soup = BeautifulSoup(page.content, 'html.parser') |
| 31 | + results = soup.find( |
| 32 | + "table", {"class": "table table--scroll-on-tablet top-players"}) |
| 33 | + rows = results.findChildren('tr') |
| 34 | + |
| 35 | + table_data = [] |
| 36 | + row_values = [] |
| 37 | + # Append player data into a list |
| 38 | + for row in rows: |
| 39 | + cells = row.findChildren(['th', 'td']) |
| 40 | + for cell in cells: |
| 41 | + value = cell.text.strip() |
| 42 | + value = " ".join(value.split()) |
| 43 | + row_values.append(value) |
| 44 | + table_data.append(row_values) |
| 45 | + row_values = [] |
| 46 | + |
| 47 | + # Formatting the data stored in the list |
| 48 | + p_records = "" |
| 49 | + for player in table_data[:51]: |
| 50 | + single_record = "" |
| 51 | + for cell in player: |
| 52 | + format_cell = "{:<20}" |
| 53 | + single_record += format_cell.format(cell[:20]) |
| 54 | + single_record += "\n" |
| 55 | + p_records += single_record |
| 56 | + |
| 57 | + # Adding the formatted data into tkinter GUI |
| 58 | + query_label.config(state=tk.NORMAL) |
| 59 | + query_label.delete(1.0,"end") |
| 60 | + query_label.insert(1.0,p_records) |
| 61 | + query_label.config(state=tk.DISABLED) |
| 62 | + |
| 63 | + |
| 64 | +# Creating tkinter window |
| 65 | +window = tk.Tk() |
| 66 | +window.title('IPL Statistics') |
| 67 | +window.geometry('800x850') |
| 68 | + |
| 69 | +# label text for title |
| 70 | +ttk.Label(window, text="IPL Statistics", |
| 71 | + background='blue', foreground="white", |
| 72 | + font=("Helvetica", 20)).grid(row=0, column=1) |
| 73 | + |
| 74 | +# label |
| 75 | +ttk.Label(window, text="Select category and year :", |
| 76 | + font=("Helvetica", 15)).grid(column=0, |
| 77 | + row=5, padx=10, pady=25) |
| 78 | + |
| 79 | +# Combobox creation |
| 80 | +category = ttk.Combobox( |
| 81 | + window, width=27, state='readonly') |
| 82 | +year = ttk.Combobox( |
| 83 | + window, width=27, state='readonly') |
| 84 | + |
| 85 | +submit_btn = ttk.Button(window, text="Search", command=scrape_results) |
| 86 | + |
| 87 | +# Adding combobox drop down list |
| 88 | +category['values'] = ('Most Runs', 'Most Fours', |
| 89 | + 'Most Sixes', 'Most Fifties', 'Most Centuries', 'Highest Scores', 'Most Wickets', 'Most Maidens', 'Most Dot Balls', 'Best Bowling Average', 'Best Bowling Economy', 'Best Bowling Strike Rate') |
| 90 | + |
| 91 | +year['values'] = ('All time','2021', '2020', '2019', '2018', '2017', '2016', |
| 92 | + '2015', '2014', '2013', '2012', '2011', '2010', '2009', '2008') |
| 93 | + |
| 94 | +category.grid(column=1, row=5, padx=10) |
| 95 | +category.current(0) |
| 96 | + |
| 97 | +year.grid(column=2, row=5, padx=10) |
| 98 | +year.current(0) |
| 99 | + |
| 100 | +submit_btn.grid(row=5, column=3, pady=5, padx=15, ipadx=5) |
| 101 | + |
| 102 | +frame = ttk.Frame(window) |
| 103 | +frame.place(relx=0.50, rely=0.12, relwidth=0.98, relheight=0.90, anchor="n") |
| 104 | + |
| 105 | +query_label = tk.Text( frame,height="52",width="500") |
| 106 | +query_label.grid(row=7, columnspan=2) |
| 107 | + |
| 108 | +window.mainloop() |
0 commit comments