Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature to extract players URL in FBREF #806

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion soccerdata/fbref.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,35 @@
.loc[self.leagues]
)

def read_player_season_stats(self, stat_type: str = "standard") -> pd.DataFrame:
def _extract_players_url(self, tree: etree.ElementTree) -> dict:
"""Extract players profile URL from the parsed HTML tree."""
player_urls = {}

# The table is often inside a comment
comments = tree.xpath("//comment()")
for comment in comments:
if "div_stats" in comment.text:
parser = etree.HTMLParser(recover=True)
table_tree = etree.fromstring(comment.text, parser)

for player_elem in table_tree.xpath("//td[@data-stat='player']/a"):
player_name = player_elem.text
player_url = player_elem.get("href")
if player_name and player_url:
player_urls[player_name] = f"https://fbref.com{player_url}"
return player_urls

# If not inside a comment, try normal extraction
for player_elem in tree.xpath("//td[@data-stat='player']/a"):
player_name = player_elem.text
player_url = player_elem.get("href")

Check warning on line 557 in soccerdata/fbref.py

View check run for this annotation

Codecov / codecov/patch

soccerdata/fbref.py#L556-L557

Added lines #L556 - L557 were not covered by tests
if player_name and player_url:
player_urls[player_name] = f"https://fbref.com{player_url}"
return player_urls

Check warning on line 560 in soccerdata/fbref.py

View check run for this annotation

Codecov / codecov/patch

soccerdata/fbref.py#L559-L560

Added lines #L559 - L560 were not covered by tests

def read_player_season_stats( # noqa: C901
self, stat_type: str = "standard", extract_players_url: bool = False
) -> pd.DataFrame:
"""Retrieve players from the datasource for the selected leagues and seasons.

The following stat types are available:
Expand All @@ -553,6 +581,8 @@
----------
stat_type :str
Type of stats to retrieve.
extract_players_url :bool
If True, the URL to player profiles will be extracted.

Raises
------
Expand Down Expand Up @@ -633,6 +663,13 @@
df_table[("Unnamed: league", "league")] = lkey
df_table[("Unnamed: season", "season")] = skey
df_table = _fix_nation_col(df_table)

if extract_players_url:
player_links = self._extract_players_url(tree)
df_table["player_url"] = df_table[("Unnamed: 1_level_0", "Player")].map(
player_links
)

players.append(df_table)

# return dataframe
Expand Down
25 changes: 21 additions & 4 deletions tests/test_FBref.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ def test_read_player_season_stats(fbref_ligue1: FBref, stat_type: str) -> None:
assert isinstance(fbref_ligue1.read_player_season_stats(stat_type), pd.DataFrame)


def test_read_player_season_stats_with_player_url(fbref_ligue1: FBref) -> None:
assert isinstance(fbref_ligue1.read_player_season_stats("standard", True), pd.DataFrame)


def test_read_schedule(fbref_ligue1: FBref) -> None:
assert isinstance(fbref_ligue1.read_schedule(), pd.DataFrame)

Expand All @@ -110,7 +114,8 @@ def test_read_schedule(fbref_ligue1: FBref) -> None:
)
def test_read_player_match_stats(fbref_ligue1: FBref, stat_type: str) -> None:
assert isinstance(
fbref_ligue1.read_player_match_stats(stat_type, match_id="796787da"), pd.DataFrame
fbref_ligue1.read_player_match_stats(stat_type, match_id="796787da"),
pd.DataFrame,
)


Expand Down Expand Up @@ -143,17 +148,29 @@ def test_read_lineup(fbref_ligue1: FBref) -> None:
def test_concat() -> None:
df1 = pd.DataFrame(
columns=pd.MultiIndex.from_tuples(
[("Unnamed: a", "player"), ("Performance", "Goals"), ("Performance", "Assists")]
[
("Unnamed: a", "player"),
("Performance", "Goals"),
("Performance", "Assists"),
]
)
)
df2 = pd.DataFrame(
columns=pd.MultiIndex.from_tuples(
[("Unnamed: a", "player"), ("Unnamed: b", "Goals"), ("Performance", "Assists")]
[
("Unnamed: a", "player"),
("Unnamed: b", "Goals"),
("Performance", "Assists"),
]
)
)
df3 = pd.DataFrame(
columns=pd.MultiIndex.from_tuples(
[("Unnamed: a", "player"), ("Goals", "Unnamed: b"), ("Performance", "Assists")]
[
("Unnamed: a", "player"),
("Goals", "Unnamed: b"),
("Performance", "Assists"),
]
)
)
res = _concat([df1, df2, df3], key=["player"])
Expand Down
Loading