Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from idlelib.iomenu import encoding
- import requests
- from bs4 import BeautifulSoup
- import csv
- from pprint import pprint
- from pandas.io.common import file_exists
- def get_match_info(championships, matches_details):
- championship_title = championships.contents[1].find('h2').text.strip()
- # print(championship_title) # الدوري المصري
- all_matches = championships.contents[3].find_all('div')
- # print(all_matches)
- number_of_matches = len(all_matches)
- # print(number_of_matches)
- for i in range(number_of_matches):
- # get teams names:
- try:
- team_A = all_matches[i].find('div', {'class': 'teamA'}).text.strip()
- except:
- continue
- team_B = all_matches[i].find('div', {'class': 'teamB'}).text.strip()
- # get score:
- match_result = all_matches[i].find('div', {'class': 'MResult'}).find_all('span', {'class': 'score'})
- score = f"{match_result[0].text.strip()} - {match_result[1].text.strip()}"
- # get match time:
- match_time = all_matches[i].find('div', {'class': 'MResult'}).find('span', {'class': 'time'}).text.strip()
- # add match info to matches_details:
- temp_Dict = {
- "Title": championship_title,
- "Team A": team_A,
- "Team B": team_B,
- "Time": match_time,
- "Result": score,
- }
- matches_details.append(temp_Dict)
- print(f"\ngetting match # {i+1} details ==>\n")
- pprint(temp_Dict)
- return matches_details
- def main():
- date = "11/22/2024" #input("Please Entera Date in the Following Format MM/DD/YYYY: ") # 12/02/2024
- page = requests.get(f"https://www.yallakora.com/match-center/?date={date}#days") # YallaKora
- src = page.content
- soup = BeautifulSoup(src, "lxml")
- matches_details = []
- # append the Keys Dict
- matches_details.append({
- "Title": "Title",
- "Team A": "Team A",
- "Team B": "Team B",
- "Time": "Time",
- "Result": "Score",
- })
- championships = soup.find_all("div", {'class' :'matchCard'}) # == []
- for championship in championships:
- matches_details = get_match_info(championship, matches_details)
- # keys = matches_details[0].keys()
- # with open('matches_details.csv', 'w') as output_file:
- # dict_writer = csv.DictWriter(f=output_file, fieldnames=["Title","Team A","Team B","Time","Result"])
- #
- # # dict_writer = csv.DictWriter(output_file.keys())
- # dict_writer.writeheader()
- # dict_writer.writerows(matches_details[1:])
- # print("File Created")
- with open('matches_details.csv', 'w', encoding='utf-8-sig', newline='') as output_file:
- dict_writer = csv.DictWriter(output_file, fieldnames=["Title", "Team A", "Team B", "Time", "Result"])
- dict_writer.writeheader()
- dict_writer.writerows(matches_details[1:])
- print("File Created")
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement