extract url parameters python

Solutions on MaxInterview for extract url parameters python by the best coders in the world

showing results for - "extract url parameters python"
Erica
28 May 2018
1#Analysing URL parameters with Python for SEO
2from pandas import DataFrame, read_csv
3import urllib.parse as urlparse
4from urllib.parse import parse_qs
5
6input_file = "spreadsheet.csv"
7output_file = "output.csv"
8url_column = "Address"
9urls = read_csv(input_file, usecols=[url_column])[url_column].unique()
10parameters = {}
11
12print(f"Analysing {len(urls)} urls ...")
13for url in urls:
14     query_string = urlparse.urlparse(url)
15     for param in parse_qs(query_string.query):
16           parameters[param] = parameters.get(param, 0) + 1
17
18parameters = [(key, value) for key, value in sorted(parameters.items(), 
19key=lambda item: item[1], reverse=True)]
20print(f"{len(parameters)} unique URL parameters found.")
21print(f"Saving output to {output_file}...") 
22DataFrame(parameters, columns=["Parameter","Count"]).to_csv(output_file, index=False)