Advertisement
kingbode

Untitled

Dec 12th, 2024
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.96 KB | None | 0 0
  1.  
  2. from idlelib.iomenu import encoding
  3. import requests
  4. from bs4 import BeautifulSoup
  5. import csv
  6. from pprint import pprint
  7.  
  8. from pandas.io.common import file_exists
  9.  
  10.  
  11. def get_match_info(championships, matches_details):
  12.     championship_title = championships.contents[1].find('h2').text.strip()
  13.     # print(championship_title) # الدوري المصري
  14.     all_matches = championships.contents[3].find_all('div')
  15.     # print(all_matches)
  16.     number_of_matches = len(all_matches)
  17.     # print(number_of_matches)
  18.     for i in range(number_of_matches):
  19.  
  20.         # get teams names:
  21.         try:
  22.             team_A = all_matches[i].find('div', {'class': 'teamA'}).text.strip()
  23.         except:
  24.             continue
  25.  
  26.         team_B = all_matches[i].find('div', {'class': 'teamB'}).text.strip()
  27.         # get score:
  28.         match_result = all_matches[i].find('div', {'class': 'MResult'}).find_all('span', {'class': 'score'})
  29.         score = f"{match_result[0].text.strip()} - {match_result[1].text.strip()}"
  30.         # get match time:
  31.         match_time = all_matches[i].find('div', {'class': 'MResult'}).find('span', {'class': 'time'}).text.strip()
  32.         # add match info to matches_details:
  33.         temp_Dict = {
  34.             "Title": championship_title,
  35.             "Team A": team_A,
  36.             "Team B": team_B,
  37.             "Time": match_time,
  38.             "Result": score,
  39.         }
  40.  
  41.         matches_details.append(temp_Dict)
  42.  
  43.         print(f"\ngetting match # {i+1} details ==>\n")
  44.         pprint(temp_Dict)
  45.  
  46.     return matches_details
  47.  
  48.  
  49.  
  50.  
  51. def main():
  52.     date = "11/22/2024" #input("Please Entera Date in the Following Format MM/DD/YYYY: ")  # 12/02/2024
  53.     page = requests.get(f"https://www.yallakora.com/match-center/?date={date}#days")  # YallaKora
  54.  
  55.     src = page.content
  56.     soup = BeautifulSoup(src, "lxml")
  57.     matches_details = []
  58.     # append the Keys Dict
  59.     matches_details.append({
  60.         "Title": "Title",
  61.         "Team A": "Team A",
  62.         "Team B": "Team B",
  63.         "Time": "Time",
  64.         "Result": "Score",
  65.     })
  66.     championships = soup.find_all("div", {'class' :'matchCard'}) # == []
  67.  
  68.     for championship in championships:
  69.         matches_details = get_match_info(championship, matches_details)
  70.         # keys = matches_details[0].keys()
  71.  
  72.     # with open('matches_details.csv', 'w') as output_file:
  73.     #     dict_writer = csv.DictWriter(f=output_file, fieldnames=["Title","Team A","Team B","Time","Result"])
  74.     #
  75.     #     # dict_writer = csv.DictWriter(output_file.keys())
  76.     #     dict_writer.writeheader()
  77.     #     dict_writer.writerows(matches_details[1:])
  78.     #     print("File Created")
  79.  
  80.     with open('matches_details.csv', 'w', encoding='utf-8-sig', newline='') as output_file:
  81.         dict_writer = csv.DictWriter(output_file, fieldnames=["Title", "Team A", "Team B", "Time", "Result"])
  82.         dict_writer.writeheader()
  83.         dict_writer.writerows(matches_details[1:])
  84.         print("File Created")
  85.  
  86.  
  87. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement