get url parameters python code example

Example: extract url parameters python

#Analysing URL parameters with Python for SEO
from pandas import DataFrame, read_csv
import urllib.parse as urlparse
from urllib.parse import parse_qs

input_file = "spreadsheet.csv"
output_file = "output.csv"
url_column = "Address"
urls = read_csv(input_file, usecols=[url_column])[url_column].unique()
parameters = {}

print(f"Analysing {len(urls)} urls ...")
for url in urls:
     query_string = urlparse.urlparse(url)
     for param in parse_qs(query_string.query):
           parameters[param] = parameters.get(param, 0) + 1

parameters = [(key, value) for key, value in sorted(parameters.items(), 
key=lambda item: item[1], reverse=True)]
print(f"{len(parameters)} unique URL parameters found.")
print(f"Saving output to {output_file}...") 
DataFrame(parameters, columns=["Parameter","Count"]).to_csv(output_file, index=False)