├── .gitignore ├── README.md ├── pagespeed-api.py └── pagespeed.txt /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .DS_Store -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | *SYNOPSIS* 2 | 1. This script reads urls from 'pagespeed.txt' file. Load this file with full URLS. 3 | 2. Queries each url with the google pagespeed api. 4 | 3. Filters JSON results to only include desired metrics. 5 | 4. Metrics are saved to local .csv spreadsheet for analysis. 6 | -------------------------------------------------------------------------------- /pagespeed-api.py: -------------------------------------------------------------------------------- 1 | import requests 2 | 3 | # Documentation: https://developers.google.com/speed/docs/insights/v5/get-started 4 | 5 | # JSON paths: https://developers.google.com/speed/docs/insights/v4/reference/pagespeedapi/runpagespeed 6 | 7 | # Populate 'pagespeed.txt' file with URLs to query against API. 8 | with open('pagespeed.txt') as pagespeedurls: 9 | download_dir = 'pagespeed-results.csv' 10 | file = open(download_dir, 'w') 11 | content = pagespeedurls.readlines() 12 | content = [line.rstrip('\n') for line in content] 13 | 14 | columnTitleRow = "URL, First Contentful Paint, First Interactive\n" 15 | file.write(columnTitleRow) 16 | 17 | # This is the google pagespeed api url structure, using for loop to insert each url in .txt file 18 | for line in content: 19 | # If no "strategy" parameter is included, the query by default returns desktop data. 20 | x = f'https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url={line}&strategy=mobile' 21 | print(f'Requesting {x}...') 22 | r = requests.get(x) 23 | final = r.json() 24 | 25 | try: 26 | urlid = final['id'] 27 | split = urlid.split('?') # This splits the absolute url from the api key parameter 28 | urlid = split[0] # This reassigns urlid to the absolute url 29 | ID = f'URL ~ {urlid}' 30 | ID2 = str(urlid) 31 | urlfcp = final['lighthouseResult']['audits']['first-contentful-paint']['displayValue'] 32 | FCP = f'First Contentful Paint ~ {str(urlfcp)}' 33 | FCP2 = str(urlfcp) 34 | urlfi = final['lighthouseResult']['audits']['interactive']['displayValue'] 35 | FI = f'First Interactive ~ {str(urlfi)}' 36 | FI2 = str(urlfi) 37 | except KeyError: 38 | print(f' One or more keys not found {line}.') 39 | 40 | try: 41 | row = f'{ID2},{FCP2},{FI2}\n' 42 | file.write(row) 43 | except NameError: 44 | print(f' Failing because of KeyError {line}.') 45 | file.write(f' & Failing because of nonexistant Key ~ {line}.' + '\n') 46 | 47 | try: 48 | print(ID) 49 | print(FCP) 50 | print(FI) 51 | except NameError: 52 | print(f' Failing because of KeyError {line}.') 53 | 54 | file.close() 55 | -------------------------------------------------------------------------------- /pagespeed.txt: -------------------------------------------------------------------------------- 1 | https://stores.uscellular.com --------------------------------------------------------------------------------