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 pathcbs.py
More file actions
executable file
·46 lines (40 loc) · 1.23 KB
/
cbs.py
File metadata and controls
executable file
·46 lines (40 loc) · 1.23 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
#!/usr/bin/env python3
from bs4 import BeautifulSoup
import requests
import csv
import os
positions = [
'QB',
'RB',
'WR',
'TE',
'K',
'DST',
]
weeks = range(1, 22)
def cbs(pos, week):
url = 'https://www.cbssports.com/fantasy/football/stats/sortable/points/{pos}/standard/projections/2017/{week}?&print_rows=9999'.format(pos=pos, week=week)
try:
contents = requests.get(url).content
soup = BeautifulSoup(contents, "html.parser")
rows = soup.find('table').find_all('tr')
header = [td.text.split(',')[0] for td in rows[2]]
data = [[td.text.split(',')[0] for td in row] for row in rows[3:-1]]
except IndexError:
print("Failed: " + pos + " " + str(week))
return
filename = 'cbs_{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('cbs')
[cbs(pos, week) for pos in positions for week in weeks]
os.chdir('..')