This repository was archived by the owner on May 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyahoo.py
More file actions
74 lines (64 loc) · 2.27 KB
/
yahoo.py
File metadata and controls
74 lines (64 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
from bs4 import BeautifulSoup
import csv
import time
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
username = "weinky"
password = ""
pages = {
'QB': 5,
'RB': 11,
'WR': 16,
'TE': 10,
}
weeks = range(1, 18)
def cbs(pos, week):
url = 'https://football.fantasysports.yahoo.com/f1/731507/players?&sort=AR&sdir=1&status=ALL&pos={pos}&stat1=S_PW_{week}&jsenabled=1&count={i}'.format(pos=pos, week=week, i="{i}")
try:
driver.get(url.format(i=0))
time.sleep(3)
elem = driver.find_element_by_class_name("Table")
soup = BeautifulSoup(elem.get_attribute("innerHTML"), "html.parser")
pheader = soup.find('thead').find_all('tr')[1].find_all('th')
header = [th.text.strip(u'\ue002') for th in pheader]
header = ['FN', 'LN'] + header[3:-1]
data = []
for i in range(pages[pos]):
driver.get(url.format(i=25 * i))
time.sleep(3)
elem = driver.find_element_by_class_name("Table")
soup = BeautifulSoup(elem.get_attribute("innerHTML"), "html.parser")
pdata = [[td.text for td in row.find_all('td')] for row in soup.find_all('tr') if len(row.find_all('td')) == len(pheader)]
data += [row[1].split('\n')[2].split(' ')[:2] + row[3:-1] for row in pdata]
except AttributeError:
print("Failed: " + pos + " " + str(week))
return
filename = 'yahoo_{pos}_week{week}.csv'.format(pos=pos, week=week)
with open(filename, 'w') as f:
print('writing: ', filename)
writer = csv.writer(f)
try:
writer.writerow(header)
writer.writerows(data)
except UnicodeEncodeError:
print("Failed: " + pos + " " + str(week))
print(header)
print(data)
return
url = 'https://football.fantasysports.yahoo.com/f1/731507/players'
driver = webdriver.Firefox()
driver.get(url)
elem = driver.find_element_by_id("login-username")
elem.clear()
elem.send_keys(username)
elem.send_keys(Keys.RETURN)
time.sleep(2)
elem = driver.find_element_by_id("login-passwd")
elem.clear()
elem.send_keys(password)
elem.send_keys(Keys.RETURN)
os.chdir('yahoo')
[cbs(pos, week) for pos in pages for week in weeks]
os.chdir('..')