Skip to content

Instantly share code, notes, and snippets.

@thayton
Last active January 24, 2023 03:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thayton/a5d0c4319d9657d1816fa94ff62e0452 to your computer and use it in GitHub Desktop.
Save thayton/a5d0c4319d9657d1816fa94ff62e0452 to your computer and use it in GitHub Desktop.
pga tour
#######################################################################
# 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