Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate dynamically URL in python

Tags:

python

url

urllib

I would like to programatically generate a URL made by these parts

Fixed part

https://booking.snav.it/#/booking/rates/

Outbound route number - changes

1040

Outbound date - changes

19-02-2019

Inbound route number - changes

1042

Inbound date - changes

20-02-2019

Parameters:

"adults": "1"
"childs":"0"
"infants":"0"
"res": "0"
"vehicle":"0"

Output

https://booking.snav.it/#/booking/rates/1040/19-02-2019/1042/19-02-2019?adults=1&childs=0&infants=0&res=0&vehicle=0

I know how to pass parameters with urllib.parse.urlencode

params = urllib.parse.urlencode({
   "adults": "1"
    "childs":"0"
    "infants":"0"
    "res": "0"
    "vehicle":"0"
})

url = "https://booking.snav.it/#/booking/rates/"
res = requests.get(url, params=params)

but don't know how to build dynamically the first part after the fixed URL 1040/19-02-2019/1042/19-02-2019

like image 649
Daniela Avatar asked Jan 28 '26 09:01

Daniela


1 Answers

A URL is really just a string, any of the usual string manipulation techniques would do here. Your component parts don't have any characters in them that would require URL-encoding here either, making the whole process simpler.

If you do have component parts that use characters that are not in the list of unreserved characters, then use the urllib.parse.quote() function to convert those characters to URL-safe components.

You could use str.join() with / to join string parts:

outbound_route = '1040'
outbound_date = '19-02-2019'
inbound_route = '1042'
inbound_date = '20-02-2019'

url = "https://booking.snav.it/#/booking/rates"  # no trailing /
final_url = '/'.join([url, outbound_route, outbound_date, inbound_route, inbound_date])

or you could use a formatted string literal:

url = "https://booking.snav.it/#/booking/rates/"
final_url = f'{url}{outbound_route}/{outbound_date}/{inbound_route}/{inbound_date}'

This approach has the advantage that the components don't have to be strings; if outbound_route and inbound_route are integers, you don't have to explicitly convert them to strings first.

Or, since URL paths work a lot like POSIX filesystem paths, you could use the pathlib.PosixPurePath() class to contruct the path:

from pathlib import PosixPurePath

path = PosixPurePath('/booking/rates') / outbound_route / outbound_date / inbound_route / inbound_date
final_url = f"https://booking.snav.it/#{path}"

In all cases, you end up with a final URL to use in requests:

res = requests.get(final_url, params=params)
like image 74
Martijn Pieters Avatar answered Jan 31 '26 03:01

Martijn Pieters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!