# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# ## Scrapen der Daten

import requests
from bs4 import BeautifulSoup
import pandas as pd

# Test für die Jahre 2016, 2017:

# +
jahre = list(range(2016,2018))

for jahr in jahre:
    url = "https://www.atpworldtour.com/en/players/roger-federer/f324/player-activity?year="
    url = url+str(jahr)
    print(url)
    r = requests.get(url)
    soup = BeautifulSoup(r.text, 'html.parser')
# -

soup.text

# Turniere, Daten und Gegner:

# +
federer = []
jahre = list(range(2016,2018))

for jahr in jahre:
    url = "https://www.atpworldtour.com/en/players/roger-federer/f324/player-activity?year="
    url = url+str(jahr)
    print(url)
    r = requests.get(url)
    soup = BeautifulSoup(r.text, 'html.parser')

    turniere = soup.find('div', {'class':"activity-tournament-table"}).find_all('a', {'class':'tourney-title'})
    daten = soup.find('div', {'class':"activity-tournament-table"}).find_all('span', {'class':'tourney-dates'})
    gegner = soup.find('div', {'class':"day-table-name "}).find_all('a', {'class':'mega-player-name'})

    for turnier, datum, spieler in zip(turniere, daten, gegner):
        turnier = turnier.text
        datum = datum.text
        gegner = spieler.text
        minidict = {'Turnier': turnier,
                    'Datum': datum,
                    "Gegner": spieler,
                    'Saison':str(jahr)}
        federer.append(minidict)

# +
federer = []
jahre = list(range(2016,2018))

for turnier in turniere:
    for jahr in jahre:
        url = "https://www.atpworldtour.com/en/players/roger-federer/f324/player-activity?year="
        url = url+str(jahr)
        print(url)
        r = requests.get(url)
        soup = BeautifulSoup(r.text, 'html.parser')

        daten = soup.find('div', {'class':"activity-tournament-table"}).find_all('span', {'class':'tourney-dates'})
        gegner = soup.find('div', {'class':"day-table-name "}).find_all('a', {'class':'mega-player-name'})

        for datum, spieler in zip(daten, gegner):
        datum = datum.text
        gegner = spieler.text
        minidict = {'Datum': datum,
                    "Gegner": spieler,
                    'Saison':str(jahr)}
        federer.append(minidict)
# -

# Test für td:

soup.find('div', {'class':"activity-tournament-table"}).find('div', {'class':'mega-table'}).find_all('td')

federer

# # Testen

r = requests.get('https://www.atpworldtour.com/en/players/roger-federer/f324/player-activity?year=2017')
soup = BeautifulSoup(r.text, 'html.parser')

soup

soup.find_all("tr")[5].find("td")

# Gegner:

soup.find('div', {'class':"day-table-name "}).find_all('a', {'class':'mega-player-name'})

name = soup.find('div', {'class':"day-table-name "}).find_all('a', {'class':'mega-player-name'})
name



# Turnier:

soup.find('div', {'class':"activity-tournament-table"}).find_all('a', {'class':'tourney-title'})

# Datum: 

soup.find('div', {'class':"activity-tournament-table"}).find_all('span', {'class':'tourney-dates'})

# Runde:

soup.find('div', {'class':"activity-tournament-table"}).find_all('table', {'class':'mega-table'})

# Resultat:



# Sieg oder Niederlage (W oder L):



# Weltranglistenposition des Gegners:









# Code für Transermarkt (zum Abschreiben):

# +
allejahre = []
jahre = list(range(2008,2019))

for jahr in jahre:
    url = "https://www.transfermarkt.ch/premier-league/startseite/wettbewerb/GB1/plus/?saison_id="
    url = url+str(jahr)
    print(url)
    headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
    r = requests.get(url, headers=headers)
    soup = BeautifulSoup(r.text, 'html.parser')
    
    vereine = soup.find_all('td', {'class':'hauptlink no-border-links show-for-small show-for-pad'})
    beträge = soup.find_all('td', {'class':'rechts show-for-small show-for-pad nowrap'})
    
    for team, wert in zip(vereine, beträge[2:][::2]):
        team = team.text
        wert_mit_punkt = wert.text.replace(",",".")
        if " Mrd" in wert_mit_punkt:
            neuwert = float(wert_mit_punkt.replace(" Mrd. €",""))*1000
        else:
            neuwert = float(wert_mit_punkt.replace(" Mio. €",""))
        wert = float(wert.text.replace(' Mio. €', '').replace(" Mrd. €","").replace(',', '.'))
        minidict = {'Team': team,
                    'Wert': neuwert,
                    'Saison':str(jahr)}
        allejahre.append(minidict)
# -

allejahre



StarcoderdataJupyterScriptsDedupFiltered