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
| # Convert to json format | |
| result_json = json.loads(result) | |
| print(result_json) | |
| with open('result.json', 'w') as outfile: | |
| json.dump(result_json, outfile) | |
| files.download('result.json') |
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
| # Define URL | |
| url = 'https://www.example.co.uk' | |
| # API request url | |
| result = urllib.request.urlopen('https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url={}/&strategy=mobile'\ | |
| .format(url)).read().decode('UTF-8') | |
| print(result) |
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
| # Import required packages | |
| import json | |
| import requests | |
| import pandas as pd | |
| import urllib | |
| import time | |
| from google.colab import files | |
| import io |
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
| uploaded = files.upload() | |
| #if your column header is something other than 'url' please define it here | |
| column_header='url' |
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
| # Get the filename from the upload so we can read it into a CSV. | |
| for key in uploaded.keys(): | |
| filename = key | |
| # Read the selected file into a Pandas Dataframe | |
| df = pd.read_csv(io.BytesIO(uploaded[filename])) | |
| df.head() | |
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
| response_object = {} | |
| # Iterate through the df | |
| for x in range(0, len(df)): | |
| # Define request parameter | |
| url = df.iloc[x][column_header] | |
| # Make request |
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
| # Create dataframe to store field data responses | |
| df_pagespeed_results = pd.DataFrame(columns= | |
| ['url', | |
| 'Overall_Category', | |
| 'Largest_Contentful_Paint', | |
| 'First_Input_Delay', | |
| 'Cumulative_Layout_Shift', | |
| 'First_Contentful_Paint', | |
| 'Time_to_Interactive', | |
| 'Total_Blocking_Time', |
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
| for (url, x) in zip( | |
| response_object.keys(), | |
| range(0, len(response_object)) | |
| ): | |
| # URLs | |
| df_pagespeed_results.loc[x, 'url'] =\ | |
| response_object[url]['lighthouseResult']['finalUrl'] | |
| # Overall Category |
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
| #Download csv file | |
| summary.to_csv('pagespeed_results.csv') | |
| files.download('pagespeed_results.csv') |
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
| #Replace the 's' with a blank space so we can turn into numbers | |
| df_pagespeed_results['Largest_Contentful_Paint'] = df_pagespeed_results.Largest_Contentful_Paint.str.replace('s', '') | |
| df_pagespeed_results['First_Contentful_Paint'] = df_pagespeed_results.First_Contentful_Paint.str.replace('s', '') | |
| df_pagespeed_results['Time_to_Interactive'] = df_pagespeed_results.Time_to_Interactive.str.replace('s', '') | |
| df_pagespeed_results['Total_Blocking_Time'] = df_pagespeed_results.Total_Blocking_Time.str.replace('ms', '') | |
| df_pagespeed_results['Speed_Index'] = df_pagespeed_results.Speed_Index.str.replace('s', '') |
OlderNewer