Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
URL String with parameters
#15
Was the question about constructing URL, or downloading content?

Parsing:
# required for parsing
from urllib.parse import urlparse, parse_qs

# required to contruct
from urllib.parse import urlunparse, urlencode

url_str = "http://example.com?param1=a&token=TOKEN_TO_REPLACE&param2=c"



# Parsing the url_str
url = urlparse(url_str)
# Convert the query to a dict
query = parse_qs(url.query)

# later to construct an url, you require following parameters:
scheme, netloc, path, params, query, fragment = url


# Construct the url with a new query
query = {"foo": ["bar", "fizz", "buzz"], "bar": [42]}
query_s = urlencode(query)

new_url = urlunparse((scheme, netloc, path, params, query_s, fragment))
# it's only one parameter which is a tuple with 6 elements
print(new_url)

query_from_url = urlparse("http://example.com?foo=%5B%27bar%27%2C+%27fizz%27%2C+%27buzz%27%5D&bar=%5B42%5D").query
print(parse_qs(query_from_url))
There is another module, called yarl which looks a bit better.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
URL String with parameters - by nikoloz - May-06-2020, 12:15 PM
RE: URL String with parameters - by pyzyx3qwerty - May-06-2020, 12:26 PM
RE: URL String with parameters - by nikoloz - May-06-2020, 12:31 PM
RE: URL String with parameters - by buran - May-06-2020, 12:35 PM
RE: URL String with parameters - by nikoloz - May-06-2020, 12:42 PM
RE: URL String with parameters - by buran - May-06-2020, 12:44 PM
RE: URL String with parameters - by nikoloz - May-06-2020, 01:12 PM
RE: URL String with parameters - by buran - May-06-2020, 01:09 PM
RE: URL String with parameters - by buran - May-06-2020, 01:13 PM
RE: URL String with parameters - by buran - May-06-2020, 01:32 PM
RE: URL String with parameters - by nikoloz - May-06-2020, 01:48 PM
RE: URL String with parameters - by nikoloz - May-09-2020, 02:13 PM
RE: URL String with parameters - by snippsat - May-09-2020, 03:04 PM
RE: URL String with parameters - by nikoloz - May-15-2020, 07:24 AM
RE: URL String with parameters - by DeaD_EyE - May-15-2020, 08:20 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020