Skip to content

URL Path Extractor with Query Parameters

代码

python
from urllib.parse import urlparse

def get_url_path(url):
    parsed_url = urlparse(url)
    path = parsed_url.path
    query = parsed_url.query
    return f"{path}?{query}" if query else path

def main():
    url = get_url_path("https://www.example.com/path?param1=value1&param2=value2")
    print(url)

if __name__ == "__main__":
    main()

Released under the MIT License.