Last active
January 24, 2023 03:31
-
-
Save thayton/a5d0c4319d9657d1816fa94ff62e0452 to your computer and use it in GitHub Desktop.
pga tour
This file contains hidden or 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
####################################################################### | |
# Setup: | |
# $ python3 -m venv venv | |
# $ source venv/bin/activate | |
# $ pip install requests | |
# Usage: | |
# $ python pgatour.py | |
####################################################################### | |
import os | |
import json | |
import requests | |
import subprocess | |
from tempfile import mkstemp | |
from urllib.parse import urlparse | |
class PgaTourScraper(object): | |
def __init__(self): | |
self.url = 'https://lbdata.pgatour.com/2020/r/012/leaderboard.json' | |
self.session = requests.Session() | |
def scrape(self): | |
resp = self.session.get('https://microservice.pgatour.com/js') | |
text = "window = {{}}; {}; console.log(window.pgatour.setTrackingUserId('id8730931'));".format(resp.text) | |
fd, path = mkstemp() | |
with os.fdopen(fd, "w") as fp: | |
fp.write(text) | |
userid = subprocess.check_output(["node", path]).strip() | |
userid = ''.join(chr(c) for c in userid) # convert to str | |
os.unlink(path) | |
# Create the URL directly as requests will percent encode the userid | |
# value causing a 403 from the server... | |
url = '{0}?userTrackingId={1}'.format(self.url, userid) | |
resp = self.session.get(url) | |
data = resp.json() | |
print(json.dumps(data, indent=2)) | |
if __name__ == '__main__': | |
scraper = PgaTourScraper() | |
scraper.scrape() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment