-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetINFO.py
60 lines (52 loc) · 2.38 KB
/
GetINFO.py
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
from selenium import webdriver
from bs4 import BeautifulSoup
from urllib.parse import urljoin
import os, json, time
class Fetch:
def __init__(self):
if not os.path.exists("data.json"):
self.data = {}
self.badGateway = "An error (502 Bad Gateway) has occurred in response to this request."
self.start()
def start(self):
self.setBrowser()
self.getTeams()
self.writeData()
self.end()
def setBrowser(self):
with open("drivePath.txt", "r") as f:
self.drivePath = f.read()[:-1]
options = webdriver.ChromeOptions()
options.add_argument("-headless")
self.browser = webdriver.Chrome(executable_path=self.drivePath,
options=options)
def getTeams(self):
url = "https://www.nba.com/teams"
self.browser.get(url)
self.data["teams"] = {}
bsObj = BeautifulSoup(self.browser.page_source, "html.parser")
teamsInfo = bsObj.findAll("div", {"class": "TeamFigure_tf__2RSkP"})
for teamInfo in teamsInfo:
teamName = teamInfo.find("a", {"class": "TeamFigure_tfMainLink__mH93D Anchor_external__Mh-vB Anchor_complexLink__2NtkO"}).get_text()
profile = teamInfo.find("a", {"class": "TeamFigureLink_teamFigureLink__3uOct Anchor_complexLink__2NtkO"})["href"]
profileLink = urljoin(url, profile)
self.data["teams"][teamName] = {"teamData":{}, "playerData": {}}
self.getTeamDatas(teamName, profileLink)
def getTeamDatas(self, teamName, link):
self.browser.get(link)
if self.browser.find_element_by_tag_name("body").text == self.badGateway:
self.getTeamDatas(teamName, link)
else:
teamINFO = self.browser.find_elements_by_class_name("TeamHeader_rankValue__1pj3i")
dataColumn = ["PPG", "RPG", "APG", "OPPG"]
for i in range(4):
self.data["teams"][teamName]["teamData"][dataColumn[i]] = teamINFO[i].text
IMG = self.browser.find_element_by_class_name("TeamHeader_teamLogoBW__QkK7w.TeamLogo_logo__1CmT9").get_attribute("src")
self.data["teams"][teamName]["teamData"]["IMG"] = IMG
def writeData(self):
with open("data.json", "w") as f:
json.dump(self.data, f)
def end(self):
self.browser.close()
if __name__ == "__main__":
Fetch()