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 pathfftoday.py
More file actions
executable file
·60 lines (54 loc) · 1.76 KB
/
Copy pathfftoday.py
File metadata and controls
executable file
·60 lines (54 loc) · 1.76 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
#!/usr/bin/env python3
from bs4 import BeautifulSoup
import requests
import csv
import os
positions = {
'QB': 10,
'RB': 20,
'WR': 30,
'TE': 40,
'K': 80,
}
pages = {
'QB': 1,
'RB': 2,
'WR': 2,
'TE': 1,
'K': 1,
}
weeks = range(1, 22)
def cbs(pos, week):
url = 'https://www.fftoday.com/rankings/playerwkproj.php?Season=2017&GameWeek={week}&PosID={pos}&cur_page={i}'.format(pos=positions[pos], week=week, i='{i}')
try:
contents = requests.get(url.format(i=0)).content
soup = BeautifulSoup(contents, "html.parser")
rows = soup.find_all('table')[9].find_all('tr')
header = [td.text.rstrip().replace(u'\xa0', u'') for td in rows[2].find_all('td')]
data = []
i = 1
while i < pages[pos]:
data += [[td.text.rstrip().replace(u'\xa0', u'') for td in row.find_all('td')] for row in rows[3:]]
contents = requests.get(url.format(i=i)).content
soup = BeautifulSoup(contents, "html.parser")
rows = soup.find_all('table')[9].find_all('tr')
i += 1
data += [[td.text.rstrip().replace(u'\xa0', u'') for td in row.find_all('td')] for row in rows[3:]]
except IndexError:
print("Failed: " + pos + " " + str(week))
return
filename = 'fftoday_{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
os.chdir('fftoday')
[cbs(pos, week) for pos in positions for week in weeks]
os.chdir('..')