Created
October 18, 2018 22:15
-
-
Save etaubman/caeb884d4a29d42c4da12460a641c0d8 to your computer and use it in GitHub Desktop.
Pull Data from TrainingPeaks into CSV File
This file contains 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
import pandas as pd | |
import json | |
from bs4 import BeautifulSoup | |
import requests | |
URL = "http://home.trainingpeaks.com/athlete/workout/UzpDb" | |
file_contents = requests.get(URL).text | |
soup = BeautifulSoup(file_contents,'html.parser') | |
script_num = len(soup.find_all('script')) - 3 | |
target_script = str(soup.find_all('script')[script_num]) | |
target_script = target_script.split(';')[0] | |
target_script = target_script.split('= ')[1] | |
json_data = json.loads(target_script) | |
headers = ['seconds_elapsed'] + json_data['workoutDetailData']['workoutSampleList']['channelSet'] | |
samples = json_data['workoutDetailData']['workoutSampleList']['samples'] | |
data = [[s['ms'] / 1000] + s['values'] for s in samples] | |
df = pd.DataFrame(data,columns=headers) | |
df.to_csv('output.csv') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment