Last active
September 30, 2022 06:41
-
-
Save seozed/a6538f6768440dbc9e3d97c99ba6e992 to your computer and use it in GitHub Desktop.
update_params_for_link
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 urllib.parse as urlparse | |
def get_url_param(url: str, param: str) -> str: | |
url_parse = urlparse.urlparse(url) | |
url_dict = dict(urlparse.parse_qsl(url_parse.query)) | |
return url_dict.get(param) |
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 urllib.parse as urlparse | |
from urllib.parse import urlencode | |
def update_url_params(url: str, params: dict) -> str: | |
""" | |
update params of url | |
:param url: | |
:param params: | |
:return: | |
""" | |
url_parse = urlparse.urlparse(url) | |
url_dict = dict(urlparse.parse_qsl(url_parse.query)) | |
url_dict.update(params) | |
url_new_query = urlencode(url_dict) | |
url_parse = url_parse._replace(query=url_new_query) | |
return urlparse.urlunparse(url_parse) | |
url = "http://stackoverflow.com/search?q=question" | |
params = { | |
'lang': 'en', 'tag': 'python', | |
'q': 'hello' | |
} | |
print(update_params(url, params)) | |
# Outputs http://stackoverflow.com/search?q=hello&lang=en&tag=python |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment