Last active
October 11, 2022 00:18
-
-
Save mrphilroth/9812627ef049274388ee682681b027d6 to your computer and use it in GitHub Desktop.
Yahoo NBA Fantasy Positions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import requests | |
import numpy as np | |
import pandas as pd | |
from lxml import html | |
base_url = "http://sports.yahoo.com/nba/teams" | |
teams = [ | |
"atlanta", | |
"boston", | |
"brooklyn", | |
"new-york", | |
"philadelphia", | |
"toronto", | |
"chicago", | |
"cleveland", | |
"detroit", | |
"indiana", | |
"milwaukee", | |
"atlanta", | |
"charlotte", | |
"miami", | |
"orlando", | |
"washington", | |
"golden-state", | |
"la-clippers", | |
"la-lakers", | |
"phoenix", | |
"sacramento", | |
"dallas", | |
"houston", | |
"memphis", | |
"new-orleans", | |
"san-antonio", | |
"denver", | |
"minnesota", | |
"oklahoma-city", | |
"portland", | |
"utah", | |
] | |
nxpath = """//*[@id="Col1-0-Team-Proxy"]/div/div[3]/div[2]/div[2]/table/tbody/tr[*]/td[2]/div/div/a""" | |
pxpath = """//*[@id="Col1-0-Team-Proxy"]/div/div[3]/div[2]/div[2]/table/tbody/tr[****]/td[3]/div/span/text()""" | |
def main(): | |
rows = [] | |
for team in teams: | |
roster_url = f"{base_url}/{team}/roster/" | |
content = requests.get(roster_url).content.decode() | |
tree = html.fromstring(content) | |
names = [n.text for n in tree.xpath(nxpath) if n.text is not None] | |
for i, name in enumerate(names): | |
sname = name.split(" ") | |
output_name = ", ".join(sname[1:] + [sname[0]]) | |
positions = [ | |
p for p in tree.xpath(pxpath.replace("****", str(i + 1))) if p != "/" | |
] | |
print(i, output_name, positions) | |
output_positions = ", ".join(positions) | |
pdict = {"name": output_name, "position": output_positions} | |
rows.append(pdict) | |
df = pd.DataFrame(rows) | |
df = df.replace(np.nan, "", regex=True) | |
df.to_csv("positions.csv", index=False) | |
if __name__ == "__main__": | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name | pos0 | pos1 | pos2 | pos3 | |
---|---|---|---|---|---|
Kent Bazemore | GF | SF | SG |