How change a parameter's value of url? Without regexps.
Now I try this, but it's long:
from urllib.parse import parse_qs, urlencode, urlsplit
url = 'http://example.com/?page=1&text=test#section'
param, newvalue = 'page', '2'
url, sharp, frag = url.partition('#')
base, q, query = url.partition('?')
query_dict = parse_qs(query)
query_dict[param][0] = newvalue
query_new = urlencode(query_dict, doseq=True)
url_new = f'{base}{q}{query_new}{sharp}{frag}'
Also, I tried by urlsplit:
parsed = urlsplit(url)
query_dict = parse_qs(parsed.query)
query_dict[param][0] = newvalue
query_new = urlencode(query_dict, doseq=True)
parsed.query = query_new
url_new = urlencode(parsed)
But on urlparsed.query = query_new
it rise error AttributeError: can't set attribute
.
page=1
topage=2
. – Flexuous